reduce js object code example
Example 1: how to flatten array with reduce in javascript
let flattened = [[0, 1], [2, 3], [4, 5]].reduce(
function(accumulator, currentValue) {
return accumulator.concat(currentValue)
},
[]
)
Example 2: creating array of objects usinng reduce js
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 3: .reduce mdn
arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])
Example 4: javascript, reduce
const myReduce = myArray.reduce((acc, item) => {
acc += item
})
Example 5: object.entries reduce
.as-console-wrapper { max-height: 100% !important; top: 0;}
Example 6: reduce mdn
array.reduce(callback( accumulator, valueNow[, index[, array]] )[, initialValue]))