Skip to content

Instantly share code, notes, and snippets.

@Aldizh Aldizh/reduce_for_objects
Last active Dec 15, 2017

What would you like to do?
// Input
const dataSource = [
{id: 48, code: "firstName", description: "John"},
{id: 49, code: "lastName", description: "Smith"},
{id: 49, code: "occupation", description: "Agent"},
{id: 47, code: "status", description: "Active"},
// Output should be as follows
const output = {
firstName: "John",
lastName: "Smith",
occupation: "Agent",
status: "Active",
// Solution
const final = dataSource.reduce((accumulator, currentObj) => {
const newObj = {};
newObj[currentObj['code']] = currentObj['description'];
return Object.assign(accumulator, newObj);
}, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.