elena1234

How to remove duplicate keys in objects - Object.assign ( JavaScript)

Dec 3rd, 2021 (edited)
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let objects = JSON.parse(input);  
  3.     let result = objects.reduce((acc, x) => Object.assign(acc, x), {});
  4.                                            // [...acc, ...x]    
  5.     console.log(result);
  6. }
  7.  
  8. solve(`[{"canMove": true},{"canMove":true, "doors": 4},{"capacity": 5}]`);
  9.  
  10. // result is {canMove: true, doors: 4, capacity: 5}ove: true, doors: 4, capacity: 5}
Add Comment
Please, Sign In to add comment