Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accumulate values in array without reducing into single value

I want to do something really similar to reduce with values in my array, but I do not want to sum it all up into one value. I want to carry on the summed up value and insert the increment into each consecutive value in the array.

Like this:

const array = [{ number: 6, value: 10 }, { number: 5, value: 12 }, { number: 2, value: 5 }]

// Do something like this
const newArray = array.reduce((accumulated, item) => {
  return {  
    number: (100 / item.number) + accumulated.value || 0,
    value: ((100 / item.number) + accumulated.value || 0) * item.value
  }
}, 0)

// But the result should be something like this
newArray = [{ number: 6, value: 10 }, { number: 30, value: 360 }, { ...and so on }]

How can this be achieved? ES6 with/without lodash preferred.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since the input and output items are one-to-one, I think using .map would be more intuitive than .reduce – you can keep an outside variable for the last object returned.

const array = [{ number: 6, value: 10 }, { number: 5, value: 12 }, { number: 2, value: 5 }]

let lastReturn = array.shift();
const newArray = [lastReturn].concat(array.map(({ number, value }) => {
  const newObj = {  
    number: (100 / number) + lastReturn.value || 0,
    value: ((100 / number) + lastReturn.value || 0) * value
  };
  lastReturn = newObj;
  return newObj;
}));
console.log(newArray);

While it’d be technically possible to use .reduce for this (you can use .reduce to achieve anything related to array iteration, .map makes more sense here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading