Files
30-seconds-of-code/snippets/reduce-successive.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

25 lines
668 B
Markdown

---
title: Array of successive values
tags: array
cover: laptop-view
firstSeen: 2018-01-24T16:38:08+02:00
lastUpdated: 2020-10-22T20:24:04+03:00
---
Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.
- Use `Array.prototype.reduce()` to apply the given function to the given array, storing each new result.
```js
const reduceSuccessive = (arr, fn, acc) =>
arr.reduce(
(res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res),
[acc]
);
```
```js
reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0);
// [0, 1, 3, 6, 10, 15, 21]
```