Menu

gitpiper

reduceSuccessive javascript Code Snippet in 2022

arrayintermediate

Last Updated: 25 September 2022

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.
const reduceSuccessive = (arr, fn, acc) => arr.reduce( (res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc] );
reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6, 10, 15, 21]

javascript snippet similar to reduceSuccessive For You in September 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️