Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. /*
  2. Follow step by step instructions to complete these
  3. multiple challenges related to arrays.
  4. */
  5.  
  6. /*
  7. This require statement conveniently brings in the entire set of user profile
  8. data for you from a neighbouring file.
  9.  
  10. You can imagine that this is your network api call delivering you data
  11. */
  12. const profiles = require("./data/profileData");
  13. const shaadiData = require("./data/shaadiData.json");
  14.  
  15.  
  16.  
  17. /*
  18. Challenge 1.1 - write a function that returns an array of fullnames i.e. each element is a string that has
  19. `title, first, last` instead of those separate fields
  20. Expected const profileNameArrays = ['fullnameOne', 'etc', 'etc'] <-- array of strings fullnames
  21. */
  22. function returnFullName(object){
  23. return object.title + " " + object.first + " " + object.last
  24. }
  25.  
  26. //var result = (profiles.map(a => a.name)).map(a => returnFullName(a))
  27.  
  28. //var result = (profiles.map(a => a.name)).map(a => (a.title) + " " + (a.first) +" " + (a.last))
  29.  
  30. var result = (profiles.map(a => (a.name.title) + " " + (a.name.first) + " " + (a.name.last)))
  31.  
  32. console.log(result)
  33.  
  34.  
  35.  
  36.  
  37. //console.log(shaadiData);
  38.  
  39. /*
  40. Challenge 1.2 - write a function that returns an array that has users grouped by nationality
  41. Expected const usersGroupedByNationality = [
  42. {
  43. 'BR': [{}, {}],
  44. 'GB': [{}, {}],
  45. ...// users grouped by other countries
  46. }
  47. ]
  48. Note: This might feel super hard. Don't get stuck on it. Try it later
  49. */
  50.  
  51. var resultGroupByCountries = []
  52.  
  53. profiles.map(function(element){
  54. if (element.nat in resultGroupByCountries){
  55. var array = resultGroupByCountries[element.nat]
  56. array.push(element)
  57. resultGroupByCountries[element.nat] = array
  58. }else{
  59. resultGroupByCountries[element.nat] = [element]
  60. }
  61. })
  62.  
  63. //console.log(JSON.stringify(resultGroupByCountries))
  64.  
  65. console.log(resultGroupByCountries)
  66.  
  67. /*
  68. Challenge 1.3 - write a function that returns a transformed array of profiles combined with photo data
  69.  
  70. Start with this data
  71. */
  72.  
  73. const profilesData = [
  74. {
  75. profile: { id: "26144385", some: "more", other: "misc" },
  76. photo_details: {
  77. photos: [{ small: "bar-1", medium: "baz-1" }]
  78. }
  79. },
  80. {
  81. profile: { id: "26144334", some: "even", other: "some more" },
  82. photo_details: {
  83. photos: [
  84. { small: "bar-2", medium: "baz-2" },
  85. { small: "fizz-2", medium: "buzz-2" }
  86. ]
  87. }
  88. }
  89. ];
  90.  
  91. var resultNewData = []
  92.  
  93. profilesData.map(function(element){
  94. var newElement = new Object()
  95. newElement['id'] = element.profile.id;
  96. newElement['some'] = element.profile.some;
  97. newElement['other'] = element.profile.other;
  98. var photosArray = element.photo_details.photos
  99. var newPhotosArray = new Array()
  100. photosArray.map(function(photo){
  101. var photoObject = new Object()
  102. photoObject['small'] = photo.small
  103. photoObject['medium'] = photo.medium
  104. newPhotosArray.push(photoObject)
  105. })
  106. newElement['photos'] = newPhotosArray
  107. resultNewData.push(newElement)
  108. })
  109.  
  110. resultNewData.map(element => console.log(element))
  111.  
  112. /* and then transform it into this shape:
  113.  
  114. const expected = [
  115. {
  116. id: "26144385",
  117. some: "more",
  118. other: "misc",
  119. photos: [
  120. {
  121. small: "bar-1",
  122. medium: "baz-1"
  123. }
  124. ]
  125. },
  126. {
  127. id: "26144334",
  128. some: "even",
  129. other: "some more",
  130. photos: [
  131. {
  132. small: "bar-2",
  133. medium: "baz-2"
  134. },
  135. {
  136. small: "fizz-2",
  137. medium: "buzz-2"
  138. }
  139. ]
  140. }
  141. ];
  142.  
  143. Note: This might feel super hard. Don't get stuck on it. Try it later
  144. */
Add Comment
Please, Sign In to add comment