Guest User

Untitled

a guest
Dec 18th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. // Input
  2. const dataSource = [
  3. {id: 48, code: "firstName", description: "John"},
  4. {id: 49, code: "lastName", description: "Smith"},
  5. {id: 49, code: "occupation", description: "Agent"},
  6. {id: 47, code: "status", description: "Active"},
  7. ]
  8.  
  9. // Output should be as follows
  10. const output = {
  11. firstName: "John",
  12. lastName: "Smith",
  13. occupation: "Agent",
  14. status: "Active",
  15. }
  16.  
  17. // Solution
  18. const final = dataSource.reduce((accumulator, currentObj) => {
  19. const newObj = {};
  20. newObj[currentObj['code']] = currentObj['description'];
  21. return Object.assign(accumulator, newObj);
  22. }, {})
  23.  
  24. console.log(final)
Add Comment
Please, Sign In to add comment