0

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

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. Btw how can be this solved with for loop ?
@Mamum can you take a look please at my other question stackoverflow.com/questions/70080974/…
2

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

1 Comment

0

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

1 Comment

can you take al ook at my other question please stackoverflow.com/questions/70080974/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.