Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. /*
  2. * @type {Array< {id: number, name:string, email: string}}
  3. */
  4.  
  5. // my code with ES6
  6.  
  7. let users = [
  8. {
  9. id:1,
  10. name: "Robbi",
  11. email: "rrcarver@gmail.com"
  12. }
  13. ];
  14.  
  15. let [
  16. {
  17. id
  18. }
  19. ] = users;
  20.  
  21. users.reduce((map, user) =>{
  22. map[id] = user;
  23. return map;
  24. }, {}); // output: { '1': { id: 1, name: 'Robbi', email: 'rrcarver@gmail.com' } }
  25.  
  26. // ES5
  27. // let users = [
  28. // {
  29. // id:1,
  30. // name: "Robbi",
  31. // email: "rrcarver@gmail.com"
  32. // }
  33. // ];
  34.  
  35. // users.reduce(function(map, user){
  36. // map[user.id] = user;
  37. // return map;
  38. // }, {}); // output: { '1': { id: 1, name: 'Robbi', email: 'rrcarver@gmail.com' } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement