Skip to content

Home

Partial sum array

Creates an array of partial sums.

const accumulate = (...nums) =>
  nums.reduce((acc, n) => [...acc, n + (acc.slice(-1)[0] || 0)], []);

accumulate(1, 2, 3, 4); // [1, 3, 6, 10]
accumulate(...[1, 2, 3, 4]); // [1, 3, 6, 10]

More like this

Start typing a keyphrase to see matching snippets.