-1

我有以下数据:

   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以及activeclub值在哪里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)

      ])
    ]
  }
4

3 回答 3

1

您可以创建一个按年龄键控的 Map,将所有计数设置为零,然后通过过滤俱乐部匹配的位置来增加这些计数:

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'}}}];

const map = new Map(data.map(({age}) => [age, { age, group: 0, active: 0 }]));
for (const {age, group, active} of data) {
    map.get(age).group += group.filter(({club}) => club === match).length;
    map.get(age).active += Object.values(active).filter(({club}) => club === match).length;
}
const result = [...map.values()];

console.log(result);

于 2019-06-18T19:54:11.307 回答
0

   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'}
        }
    }
]

const acc = {};

data.forEach(d => {
    if(!acc[d.age]){
        acc[d.age] = {age: d.age, group: 0, active: 0 }
    }
    acc[d.age].group += d.group.filter(g => g.club == match).length;
    acc[d.age].active += Object.values(d.active).filter(a => a.club == match).length;
});

const res = Object.values(acc);

console.log(res);

于 2019-06-18T19:50:55.707 回答
0

此代码段将执行您想要的操作。

    const match = 'A'

    const data = [
        {
            age: "12", 
            group: [
                {club: 'A'},
                {club: 'B'}
            ],
            active: {
                'sjbuas777': {club: 'A'},
                '22kk7dgs': {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'}
            }
        }
    ]
    
    function clubMatch(acc, obj) {
      return obj.club === match ?  acc + 1 : acc
    }
    
    output = []
    temp = {}
    data.forEach(function(obj) {
      if (output.includes(obj.age)) {
        temp[obj.age] = {
          group: obj.group.reduce(clubMatch, temp[obj.age].group),
          active: Object.values(obj.active).reduce(clubMatch, temp[obj.age].active)
        }
      } else {
        output.push(obj.age)
        temp[obj.age] = {
          group: obj.group.reduce(clubMatch, 0),
          active: Object.values(obj.active).reduce(clubMatch, 0)
        }
      }
    })

    output.sort()
    output = output.map((age) => {
      return {
        age: age,
        group: temp[age].group,
        active: temp[age].active
      };
    })
    
    console.log(output)

于 2019-06-18T20:01:21.127 回答