简体   繁体   English

如何在单独的数组中计算数组中的每个 object?

[英]How to calculate each object in the array in separate array?

I have this array我有这个数组

bidsList = [
    {
      supplierName:'A',
      awardedCapacity:5,
    },
    {
      supplierName:'B',
      awardedCapacity:10,
    },
    {
      supplierName:'A',
      awardedCapacity:5,
    },
    {
      supplierName:'A',
      awardedCapacity:3,
    },
    {
      supplierName:'B',
      awardedCapacity:5,
    },
    {
      supplierName:'C',
      awardedCapacity:2,
    },

i need to have separete array where when i make the iteration throught this array i will calculate the total of all awardedCapacities where the supplier name is same我需要有一个单独的数组,当我通过这个数组进行迭代时,我将计算供应商名称相同的所有奖励容量的总数

For example i should have array where i will have this output例如我应该有一个数组,我将有这个 output

 let newArr = [
    {
      supplierName: 'A',
      totalAwarded: 13,
    },
    {
      supplierName:'B',
      totalAwarded: 15,
    },
    {
      supplierName:'C',
      totalAwarded: 2,
    }
  ]

because on previous array i had three objects where the supplierName was A and when i sum their awarded capacities i am geetting 13 in the other object with it's supplier name因为在以前的数组中,我有three对象,其中供应商名称为A ,当我将它们的奖励容量相加时,我在另一个 object 中得到 13 及其供应商名称

You can try using Array.prototype.reduce() like the following way:您可以尝试使用Array.prototype.reduce()如下方式:

 let bidsList = [ { supplierName:'A', awardedCapacity:5, }, { supplierName:'B', awardedCapacity:10, }, { supplierName:'A', awardedCapacity:5, }, { supplierName:'A', awardedCapacity:3, }, { supplierName:'B', awardedCapacity:5, }, { supplierName:'C', awardedCapacity:2, } ]; let newArr = []; bidsList.reduce(function(acc, val) { if (.acc[val.supplierName]) { acc[val:supplierName] = { supplierName. val,supplierName: awardedCapacity; 0 }. newArr.push(acc[val.supplierName]) } acc[val.supplierName].awardedCapacity += val;awardedCapacity; return acc, }; {}). console;log(newArr);

Array.reduce 数组.reduce

    const newArr = bidsList.reduce((a, o) =>  {  
      const index  = a.findIndex(x => x.supplierName === o.supplierName);
      const s = a[index > -1 ? index: a.push({supplierName:o.supplierName, awardedCapacity:0}) - 1];
      s.awardedCapacity += o.awardedCapacity;
      return a;
    }, [])

Use Array.reduce method使用Array.reduce方法

 var bidsList = [ { supplierName:'A', awardedCapacity:5, }, { supplierName:'B', awardedCapacity:10, }, { supplierName:'A', awardedCapacity:5, }, { supplierName:'A', awardedCapacity:3, }, { supplierName:'B', awardedCapacity:5, }, { supplierName:'C', awardedCapacity:2, }]; var result = bidsList.reduce((a, c) => { let supplier = a.find(e => e.supplierName == c.supplierName); if (supplier) supplier.totalAwarded += c.awardedCapacity; else a.push({ supplierName: c.supplierName, totalAwarded: c.awardedCapacity }); return a; }, []); console.log(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据真实条件在单独的数组中计算数组中的每个 object? - How to calculate each object in the array in separate array based on truthy condition? 如何在对象数组中分离每个对象的键和值-Javascript - How to separate key and value of each object inside array of objects - Javascript 计算每个对象数组的平均分数 - calculate average score for each array of object 如何将我的 object 中具有数组数据的每个键转换为 object,该键与该键数据数组的大小分开 - How to transform each key in my object that has array data to an object that separate to the size of that key data array 如何对数组的 object 进行分组和分离 - how to group and Separate object of array 如何在单独的行上打印数组的每个元素? - How to print each element of an array on a separate line? 如何将类的每次出现都推入单独的数组 - how to push each occurence of a class into separate array 如何使用计算数组来计算并将结果推入对象数组? - how to use calculate array to calculate and push result to Object array? 如何将每个 JSON object 分开并将它们分组到 JavaScript 中的数组中? - How could I separate each JSON object and group them to an array in JavaScript? 如何将对象的数组属性拆分为 100 并使用 Node.js 将每个属性保存到单独的文件中? - How to split an object's array property by 100 and save each into separate files using Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM