【问题标题】:Filter an array of repeated objects and get the sum of them过滤一组重复对象并得到它们的总和
【发布时间】:2020-02-16 18:54:49
【问题描述】:

所以我有了下一个信息,我想要实现的是过滤对象数组并将重复对象的数量相加

//Original Data
var data = {
    sublists = [
    { item: 1, qty: 2},
    { item: 2, qty: 2},
    { item: 3, qty: 2},
    { item: 1, qty: 5}
    { item: 3, qty: 3}
    ],
    ...
};
//This is what I want to achieve
var result = {
    sublists = [
    { item: 1, qty: 7},
    { item: 2, qty: 2},
    { item: 3, qty: 5}

    ],
    ...
};

到目前为止,我得到的是重复的项目名称和具有此变量数据的对象

var repeatedObjects = {
  1: [{ item: 1, qty: 2}, { item: 1, qty: 5}],
  3: [{ item: 3, qty: 2}, { item: 3, qty: 3}]
}
var repeatedItems = [1, 3];

但我坚持减少对象,这样我就可以得到原始数据,只有一个重复的对象和所有对象的总和。有什么想法吗?

【问题讨论】:

    标签: javascript arrays sorting


    【解决方案1】:

    创建一个 itemByFreq json 对象,如下所示

    var data = {
        sublists : [
        { item: 1, qty: 2},
        { item: 2, qty: 2},
        { item: 3, qty: 2},
        { item: 1, qty: 5},
        { item: 3, qty: 3}
        ]
    };
    
    var itemByFreq = {};
    for(var i=0;i<data.sublists.length;i++){
    var ji = data.sublists[i];
    itemByFreq[ji.item] = (itemByFreq[ji.item] || 0) + ji.qty;
    }
    
    console.log(itemByFreq);
    
    var duplicateItems = [];
    for(var key in itemByFreq)
    	duplicateItems.push({item: key,qty: itemByFreq[key]});
    
    console.log(duplicateItems);

    【讨论】:

      【解决方案2】:

      可以使用 Mapreduce 来完成 :)

      const dataSample = {
          sublists: [
            { item: 1, qty: 2},
            { item: 2, qty: 2},
            { item: 3, qty: 2},
            { item: 1, qty: 5},
            { item: 3, qty: 3}
          ],
      };
      
      const mergeItems = list => {
        return [...list.reduce((acc, val) => {
          const oldVal = acc.get(val.item) || { qty: 0 }
          const newVal = {
            ...val,
            qty: oldVal.qty + val.qty
          }
          acc.set(val.item, newVal)
          return acc
        }, new Map()).values()]
      }
      
      console.log(mergeItems(dataSample.sublists))

      【讨论】:

        【解决方案3】:

        如果你想在一行中使用lodash,你也可以使用这个

        const _ = require("lodash");
        
        const data = [
          {"item": 1, "qty": 2},
          {"item": 2, "qty": 2},
          {"item": 3, "qty": 2},
          {"item": 1, "qty": 5},
          {"item": 3, "qty": 3}
        ];
        
        const result = _.map(_.groupBy(data, i => i.item), (o, idx) => {
          return {"item": idx, "qty": _.sumBy(o, i => i.qty)};
        });
        
        console.log(result);
        
        

        【讨论】:

          【解决方案4】:

          使用reduce 会简化

          const update = data =>
            Object.values(
              data.sublists.reduce((acc, curr) => {
                acc[curr.item] =
                  curr.item in acc
                    ? { ...acc[curr.item], qty: acc[curr.item].qty + curr.qty }
                    : { ...curr };
                return acc;
              }, {})
            );
          
          var data = {
            sublists: [
              { item: 1, qty: 2 },
              { item: 2, qty: 2 },
              { item: 3, qty: 2 },
              { item: 1, qty: 5 },
              { item: 3, qty: 3 }
            ]
          };
          
          const res = { sublists: update(data) };
          
          console.log(res);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-30
            • 2023-02-03
            • 1970-01-01
            • 2021-12-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多