数组中多个对象的总和值

Sum values for multiple objects in the array

这是我的:

const arrayA = [{name:'a', amount: 10, serviceId: '23a', test:'SUCCESS'},
                {name:'a', amount: 9, test:'FAIL'}, 
                {name:'b', amount: 15, serviceId: '23b', test:'SUCCESS'}]

 

(注意有些对象没有'serviceId')

我想得到:

 [{name:'a', amount: 19, test:'FAIL'},
  {name:'b', amount: 15, test:'SUCCESS'}]

我已经搜索过,并尝试过这样的事情: (参考:

const result = Object.values(arrayA.reduce((r, o) => (r[o.name]
  ? (r[o.name].amount += o.amount)
  : (r[o.name] = {...o}), r), {}));

但仍不确定如何分配 test 字段。

如有任何帮助,我们将不胜感激!

您需要检查 test 并在必要时更新值。

const
    array = [{ name: 'a', amount: 10, serviceId: '23a', test: 'SUCCESS' }, { name: 'a', amount: 9, test: 'FAIL' }, { name: 'b', amount: 15, serviceId: '23b', test: 'SUCCESS' }],
    result = Object.values(array.reduce((r, { name, amount, test }) => {
        if (!r[name]) r[name] = { name, amount: 0, test };
        r[name].amount += amount;
        if (test === 'FAIL') r[name].test = 'FAIL';
        return r;
    }, {}));

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

这个?只是测试 .test 是否已经失败

const arrayA = [{ name: 'a', amount: 10, serviceId: '23a', test: 'SUCCESS' }, { name: 'a', amount: 9, test: 'FAIL' }, { name: 'b', amount: 15, serviceId: '23b', test: 'SUCCESS' }],
  result = Object.values(
    arrayA.reduce((r, o) => {
      r[o.name] ? (r[o.name].amount += o.amount) : (r[o.name] = { ...o })
      r[o.name].test = r[o.name].test === "FAIL" ? "FAIL" : o.test;
      return r;
    }, {})
  );

console.log(result);

将总和和最终值存储在对象中,然后将它们转换为数组。

const arrayA = [
  { name: "a", amount: 10, serviceId: "23a", test: "SUCCESS" },
  { name: "a", amount: 9, test: "FAIL" },
  { name: "b", amount: 15, serviceId: "23b", test: "SUCCESS" },
];

const map = {};

arrayA.forEach((row) => {
  if (!map[row.name]) {
    map[row.name] = { name: row.name, amount: 0, test: "SUCCESS" };
  }

  map[row.name].amount += row.amount;

  if (row.test === "FAIL") {
    map[row.name].test = "FAIL";
  }
});

const result = Object.values(map);

console.log(result);

你可以使用Array.reduce方法:

const arrayA=[{name:"a",amount:10,serviceId:"23a",test:"SUCCESS"},{name:"a",amount:9,test:"FAIL"},{name:"b",amount:15,serviceId:"23b",test:"SUCCESS"}];

let result  = arrayA.reduce((aac,{name,amount,test}) => {
    let idx = aac.findIndex(n => n.name === name)
    if( idx != -1){
        aac[idx].amount += amount;
        if(test === "FAIL")
            aac[idx].test = "FAIL"        
        return aac
    }
    aac.push({name,amount,test})
    return aac
},[])

console.log(result);
console.log(arrayA)

这是关于如何在 js 中处理数据的一个很好的用例,特别是像 mapreduce 这样的函数如何帮助您进行处理。这是循环和迭代的一个很好的替代方法。

此外,我建议您按照您在描述中定义的步骤进行处理。例如:

const arrayA = [
    {name:'a', amount: 10, serviceId: '23a', test:'SUCCESS'},
    {name:'a', amount: 9, test:'FAIL'},
    {name:'b', amount: 15, serviceId: '23b', test:'SUCCESS'}
]

// First group the items by name
const byName = arrayA.reduce((ob, item) => {
    if(!(item.name in  ob))
        ob[item.name] = []
    ob[item.name].push({amount: item.amount, test: item.test})
    return ob
}, {})

// Then compute the total amount in each group and find if there is any FAIL in a group
const sumByName = Object.keys(byName).map(name => { // This is a way to iterate through the groups
    // Sum the amount in all elements of a group
    const amount = byName[name].reduce((sum, item) => sum + item.amount , 0)
    // Find if there is any FAIL in a group
    const test = byName[name].map(item => item.test) // Get an array with only the test string
        .includes('FAIL') ? 'FAIL': 'SUCCESS' // Evaluate if the array includes FAIL
    return ({name, amount, test})
})

console.log(sumByName)

最后,我建议您在地图上观看这些视频并减少(以及该频道的所有内容)