[Vuejs]-Calculation sum of objects in array

0👍

This is a good case for array.reduce. It would look something like this:

const totalRealizedPL => data.reduce(
  (subtotal, item) => subtotal + item.realizedPL, // accumulator function
  0 // initial value
);
// generate some data for demo purposes
const data = Array.from({length: 5}, () => ({ value: Math.floor(Math.random() * 100) }));

const sum = data.reduce((subtotal, item) => subtotal + item.value, 0);

console.log(sum);
console.log(data);

Leave a comment