Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
- }, {})
- console.log(final)
Add Comment
Please, Sign In to add comment