Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

arry to object using reduce

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]};
}, {});
Comment

reduce method in javascript array of bjects

var arr = [{x:1}, {x:2}, {x:4}];
arr.reduce(function (acc, obj) { return acc + obj.x; }, 0); // 7
console.log(arr);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get checked value of checkbox in jquery 
Javascript :: binarycent login 
Javascript :: Use Destructuring Assignment with the Rest Operator to Reassign Array Elements 
Javascript :: axios send file 
Javascript :: patch request javascript 
Javascript :: as it does not contain a package.json file. react 
Javascript :: how to add a class to an element in javascript 
Javascript :: react select npm 
Javascript :: dom full form 
Javascript :: javascript randint 
Javascript :: webpack env argument 
Javascript :: jquery declare variable 
Javascript :: create app with a specific react version 
Javascript :: change class Name in react 
Javascript :: math .round 
Javascript :: node js check type of variable 
Javascript :: javascript max array 
Javascript :: react bootstrap add progress bar 
Javascript :: jest input value 
Javascript :: angular ng default scss 
Javascript :: random letter from a name js 
Javascript :: change value of variable javascript 
Javascript :: new line javascript 
Javascript :: how to use a specific node version for inside a folder 
Javascript :: get user time using timezone javascript 
Javascript :: node.js express 
Javascript :: get element by id jqueryt 
Javascript :: map of filtered data react 
Javascript :: generate express js project 
Javascript :: how is javascript compiled 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =