格式化具有各种数据结构的对象的数组

时间:2019-06-18 19:15:17

标签: javascript ecmascript-6 filter reduce es6-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'}
        }
    }
]

我如何按年龄对数据进行分组并给出groupsactive的计数,其中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)

      ])
    ]
  }

3 个答案:

答案 0 :(得分: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'}}}];

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);

答案 1 :(得分: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);

答案 2 :(得分: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)