0

Explain me mechanism of reduce()?

let list = [1, 2, 3, 4]; let ans = list.reduce(function (a, b) { return (a + b); }, 5); console.log(ans);

6th Sep 2018, 7:37 PM
pro
pro - avatar
1 Odpowiedź
+ 2
The job of the reduce function is to "reduce" an array into a single value (e.g. sum of all it's values) it takes in a function, that function should have a first parameter which is the accumulator (the single value that gets eventually returned) and a second one which is current array element, there are 2 other the link mentions. Get more information here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
6th Sep 2018, 7:45 PM
TurtleShell
TurtleShell - avatar