如何对对象数据进行求和和分组?

3

如上所述,我如何对对象数据进行求和和分组?
我尝试使用forEachObject.key()来对数据进行分组。
但我对此不是太熟悉。
这是我的示例数据:

const srcArr =[
    {
        "1" : {
            "60" : {
                "point" : 10,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    },
    {
        "2" : {
            "60" : {
                "point" : 100,
                "count" : 2
            }
        }
    },
    {
        "1" : {
            "88" : {
                "point" : 50,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    },

我希望得到这样的数据:
{ 
  '1': { '60': { money: 10, count: 1 }, 
         '68': { money: 40, count: 2 }, 
         '88': { money: 50, count: 1 }},
  '2': { '60': { money: 100, count: 2 }}
}

超出话题:您的数据结构看起来非常混乱,难以阅读和理解,不够“用户友好”,并且随着时间的推移会变得越来越糟糕(这与代码无关,而是与信息架构有关)。对象名称是数字?(具有“零”语义),例如键“1”出现了两次(为什么?)...(想象一下100个对象看起来像这样)。 - Ezra Siton
这些数据来自数据库,因此键“1”出现两次,并且对象名称是字符串。 - Benji
2个回答

3

试试这个

cc = [
    {
        "1" : {
            "60" : {
                "point" : 10,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    },
    {
        "2" : {
            "60" : {
                "point" : 100,
                "count" : 2
            }
        }
    },
    {
        "1" : {
            "88" : {
                "point" : 50,
                "count" : 1
            },
            "68" : {
                "point" : 20,
                "count" : 1
            },
        }
    }
]
const result = cc.reduce((arr, o) =>{
    let k = Object.keys(o)[0];
   arr[k] = arr[k] || {};
   let opo = o[k];
   if(arr[k]) {
    Object.keys(arr[k]).map(kk => {
      Object.keys(o[k]).map(cc => {
        if(kk === cc) {
          opo[cc] = opo[cc] || {};
          opo[cc].count =   o[k][cc].count + arr[k][kk].count;
          opo[cc].point =   o[k][cc].point + arr[k][kk].point;
        }
      });
   });
   }        
   arr[k] = {...arr[k], ...opo};    
   return arr;
}, {});

console.log(result);

也许这正是您所期望的。 忽略键的命名规范:p

2
我会使用 Array.reduce

const srcArr = [{
    "1": {
      "60": {
        "point": 10,
        "count": 1
      },
      "68": {
        "point": 20,
        "count": 1
      },
    }
  },
  {
    "2": {
      "60": {
        "point": 100,
        "count": 2
      }
    }
  },
  {
    "1": {
      "88": {
        "point": 50,
        "count": 1
      },
      "68": {
        "point": 20,
        "count": 1
      },
    }
  }
]

let res = srcArr.reduce((arr, o) => {
  // get the key: 1,2, or 3 etc
  let k = Object.keys(o)[0];
  // create a new object with that key if it does not exist
  arr[k] = arr[k] || {};
  // push the objects
  arr[k] = Object.assign(arr[k], o[k]);
  return arr;
}, {});

console.log(res);


但是如果我使用Object.assign(),同样会被覆盖,例如:"68"。 - Benji

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接