Example 1: reduce javascript
const sum = array.reduce((accumulator, element) => {
return accumulator + element;
}, 0);
const product = array.reduce((accumulator, element) => {
return accumulator * element;
}, 1);
Example 2: js reduce
let newArray = array.reduce( (acc, element, index, originalArray) => {
}, initialValue)
Example 3: reduce javascript
let sum = arr.reduce((curr, index) => curr + index);
Example 4: syntax of reduce in js
[1,2,3,4,5].reduce((acc, current)=>acc+current, 0)
Example 5: reduce array to object javascript
const posts = [
{id: 1, category: "frontend", title: "All About That Sass"},
{id: 2, category: "backend", title: "Beam me up, Scotty: Apache Beam tips"},
{id: 3, category: "frontend", title: "Sanitizing HTML: Going antibactirial on XSS attacks"}
];
const categoryPosts = posts.reduce((acc, post) => {
let {id, category} = post;
return {...acc, [category]: [...(acc[category] || []), id]};
}, {});
Example 6: .reduce mdn
arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])