如果对象的其他值相同,则JavaScript SUM对象值

时间:2018-10-18 20:49:47

标签: javascript arrays

对于JS高级数组方法我还是很陌生,我有一个包含成本类别和值的对象数组,如下所示:

[{category: "Bars", amount: 31231},
{category: "Transport", amount: 1297},
{category: "Utilities", amount: 12300},
{category: "Bars", amount: 2000},
{category: "Transport", amount: 2500},
{category: "Education", amount: 21321}]

我的目标是减少此数组并求和'amount'值,如下所示:

[{category: "Bars", amount: 33231},  //31231+2000
{category: "Transport", amount: 3797}, //1297+2500
{category: "Utilities", amount: 12300},
{category: "Education", amount: 21321}]

我尝试了reduce()和forEach(),但是我真的找不到解决此问题的方法。 谢谢!

5 个答案:

答案 0 :(得分:1)

您应该使用id parent 1 0 2 0 3 1 4 2 5 3 6 3 7 4 方法来执行此操作,因此将遍历数组中的项,对于每个项,您将询问该类别是否不存在于reduce中,然后使用进行初始化0值,然后求和。

newObject

答案 1 :(得分:0)

var items = [{category: "Bars", amount: 31231},
{category: "Transport", amount: 1297},
{category: "Utilities", amount: 12300},
{category: "Bars", amount: 2000},
{category: "Transport", amount: 2500},
{category: "Education", amount: 21321}];


var itemsByName = items.reduce(function (map, item) {
  
  var summarizedItem = map.get(item.category);
  if (!summarizedItem) {
    summarizedItem = item;
  } else {
    summarizedItem.amount += item.amount;
  }
  
  map.set(item.category, summarizedItem);
  
  return map;
}, new Map());

console.log(Array.from(itemsByName.values()))

答案 2 :(得分:0)

您可以通过对对象使用finc来减少数组,并更新或向结果集中添加新数组。

var array = [{ category: "Bars", amount: 31231 }, { category: "Transport", amount: 1297 }, { category: "Utilities", amount: 12300 }, { category: "Bars", amount: 2000 }, { category: "Transport", amount: 2500 }, { category: "Education", amount: 21321 }],
    result = array.reduce((r, { category, amount }) => {
        var temp = r.find(o => o.category === category);
        if (temp) {
            temp.amount += amount;
        } else {
            r.push({ category, amount });
        }
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

尝试一下:

var items = [{category: "Bars", amount: 31231},
{category: "Transport", amount: 1297},
{category: "Utilities", amount: 12300},
{category: "Bars", amount: 2000},
{category: "Transport", amount: 2500},
{category: "Education", amount: 21321}];

// get all categories names
var categories = items.map(item => item.category).filter((value, index, self) => self.indexOf(value) === index);

// create a new object with sum of amounts
var result = categories.map(item => { 
  return { 
     category: item, 
     amount: items.filter(c => c.category === item).reduce((accum, curr) => accum + curr.amount, 0) 
  } 
});

答案 4 :(得分:0)

带有Array.prototype.reduceObject.keys的简短ES6版本:

const data = [{category: "Bars", amount: 31231},{category: "Transport", amount: 1297},{category: "Utilities", amount: 12300},{category: "Bars", amount: 2000},{category: "Transport", amount: 2500},{category: "Education", amount: 21321}];

const grouped = data.reduce((all, {category: c, amount: a}) =>
    ({...all, [c]: (all[c] || 0) + a }), {});

const result = Object.keys(grouped).map(k => ({category: k,amount: grouped[k] }));

console.log(result);