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);
1 Answer
+ 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