Home > other >  How to calculate each object in the array in separate array?
How to calculate each object in the array in separate array?

Time:11-24

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

 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

CodePudding user response:

You can try using Array.prototype.reduce() like the following way:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Array.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;
    }, [])

CodePudding user response:

Use Array.reduce method

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related