我有以下数据:
const match = 'A'
const data = [
{
age: "12",
group: [
{club: 'A'},
{club: 'B'}
],
active: {
sjbuas777: {club: 'A'},
122kk7dgs: {club: 'B'}
}
},
{
age: "11",
group: [
{club: 'A'},
{club: 'D'}
],
active: {
djs7skww3: {club: 'C'},
sjhsjs72k: {club: 'B'}
}
},
{
age: "12",
group: [
{club: 'A'},
{club: 'D'}
],
active: {
shshs6kka: {club: 'A'},
5d8jallo1: {club: 'C'}
}
},
{
age: "13",
group: [
{club: 'C'},
{club: 'S'}
],
active: {
kks7sjkla: {club: 'A'},
jjs66s5as: {club: 'B'}
}
}
]
如何按年龄对数据进行分组并计算该年龄的值groups
以及active
该club
值在哪里match
?请注意,活动对象中的键是未知的。
所需的输出是:
[
{age: "11", group: 1, active: 0},
{age: "12", group: 2, active: 2},
{age: "13", group: 0, active: 1}
]
提前谢谢了。
编辑
我想展示我目前所处的位置:
function getCountsByAge() {
return [
...new Set([
...Object.values(data)
.filter(d => d.group
.some(({club}) => club === match))
.map(result => result)
])
]
}