Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. // INPUT
  2. let people = [
  3. {
  4. "firstname": "John",
  5. "lastname": "Doe",
  6. "age": 20,
  7. "address": {
  8. "city": "San Francisco",
  9. "state": "CA"
  10. }
  11. },
  12. {
  13. "firstname": "Bob",
  14. "lastname": "Lee",
  15. "age": 30,
  16. "address": {
  17. "city": "New York",
  18. "state": "NY"
  19. }
  20. },
  21. ];
  22.  
  23. // COMBINE FIRST AND LAST NAME INTO NEW JSON PROPERTY
  24. results = people.map(function (currentValue, index, arr) {
  25. // add a property to each object
  26. currentValue.fullname = currentValue.firstname + " " + currentValue.lastname;
  27. return currentValue;
  28. });
  29.  
  30. // OUTPUT
  31. /*[
  32. {
  33. "firstname": "John",
  34. "lastname": "Doe",
  35. "age": 20,
  36. "address": {
  37. "city": "San Francisco",
  38. "state": "CA"
  39. },
  40. "fullname": "John Doe"
  41. },
  42. {
  43. "firstname": "Bob",
  44. "lastname": "Lee",
  45. "age": 30,
  46. "address": {
  47. "city": "New York",
  48. "state": "NY"
  49. },
  50. "fullname": "Bob Lee"
  51. },
  52. ]*/
Add Comment
Please, Sign In to add comment