0

I have an array like this:

const list = [
    {
      quantity: 100,
      cost: 100,
      fee: 100,
    },
    {
      quantity: 200,
      cost: 200,
      fee: 200,
    },
    {
      quantity: 300,
      cost: 300,
      fee: 300,
    },
  ];

I would like to make a function that loops through it to get the total of each object's key like this:

  const cal = (item) => {
    let total = 0;
    for (let i = 0; i < list.length; i++) {
      total = list[i].item + total;
    }
    return total;
  };

So that I could run something like this:

console.log(cal(quantity));
console.log(cal(cost));
console.log(cal(fee));

Is the a way to achieve this please? I appreciate it!

Tui
  • 105
  • 1
  • 14

0 Answers0