简体   繁体   English

如何根据真实条件在单独的数组中计算数组中的每个 object?

[英]How to calculate each object in the array in separate array based on truthy condition?

I have this array我有这个数组

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

i needed 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,
    }
  ]

The solution for this is:解决方案是:

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);

but now i need to check also if the switcherStatus is setted to true only - if it is setted to false i should not calculate it's awardedCapacity and i should not push into the array if it is the only one object但现在我还需要检查 switcherStatus 是否仅设置为 true - 如果它设置为 false,我不应该计算它的奖励容量,如果它是唯一的一个 object,我不应该推入数组

so the output should be所以 output 应该是

 let newArr = [
    {
      supplierName: 'A',
      totalAwarded: 8,
    },
    {
      supplierName:'B',
      totalAwarded: 15,
    },
  ]

C IS excluded here because it is switcherStatus false, and A is 8 - because on object was with switcherStatus of false C在这里被排除,因为它是 switcherStatus 假,而 A 是 8 - 因为在 object 上是 switcherStatus 为假

i can't find a way to modify this reduce code here for that purpose.为此,我找不到在这里修改此减少代码的方法。

Just add a condition in your reduce function只需在您的reduce function 中添加一个条件

 var bidsList = [{ supplierName: 'A', awardedCapacity: 5, switcherStatus: true }, { supplierName: 'B', awardedCapacity: 10, switcherStatus: true, }, { supplierName: 'A', awardedCapacity: 5, switcherStatus: false, }, { supplierName: 'A', awardedCapacity: 3, switcherStatus: true, }, { supplierName: 'B', awardedCapacity: 5, switcherStatus: true, }, { supplierName: 'C', awardedCapacity: 2, switcherStatus: false, } ]; var result = bidsList.reduce((a, c) => { if (c.switcherStatus) { 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM