xxxxxxxxxx
const FRUITS = ["banana", "apple", "orange", "banana", "orange", "apple", "apple", "orange", "orange", "banana", "orange", "banana"]
const total = FRUITS.reduce((map, fruit) => ({
map,
[fruit]: (map[fruit] || 0) + 1,
}), {})
console.log(total) // { banana: 4, apple: 3, orange: 5}
xxxxxxxxxx
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]};
}, {});