
array.reduce() – Reduces the array to a **single value** by executing the callback on each element and accumulating the result.
๐ Syntax
result = array.reduce((accumulator, current, index, array) => {
return /* next accumulator */;
}, initialValue);
๐งช Example
const numbers = [1,2,3,4];
const sum = numbers.reduce((acc, n) => acc + n, 0);
console.log(sum); // 10
Comments
Post a Comment