Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Follow step by step instructions to complete these
- multiple challenges related to arrays.
- */
- /*
- This require statement conveniently brings in the entire set of user profile
- data for you from a neighbouring file.
- You can imagine that this is your network api call delivering you data
- */
- const profiles = require("./data/profileData");
- const shaadiData = require("./data/shaadiData.json");
- /*
- Challenge 1.1 - write a function that returns an array of fullnames i.e. each element is a string that has
- `title, first, last` instead of those separate fields
- Expected const profileNameArrays = ['fullnameOne', 'etc', 'etc'] <-- array of strings fullnames
- */
- function returnFullName(object){
- return object.title + " " + object.first + " " + object.last
- }
- //var result = (profiles.map(a => a.name)).map(a => returnFullName(a))
- //var result = (profiles.map(a => a.name)).map(a => (a.title) + " " + (a.first) +" " + (a.last))
- var result = (profiles.map(a => (a.name.title) + " " + (a.name.first) + " " + (a.name.last)))
- console.log(result)
- //console.log(shaadiData);
- /*
- Challenge 1.2 - write a function that returns an array that has users grouped by nationality
- Expected const usersGroupedByNationality = [
- {
- 'BR': [{}, {}],
- 'GB': [{}, {}],
- ...// users grouped by other countries
- }
- ]
- Note: This might feel super hard. Don't get stuck on it. Try it later
- */
- var resultGroupByCountries = []
- profiles.map(function(element){
- if (element.nat in resultGroupByCountries){
- var array = resultGroupByCountries[element.nat]
- array.push(element)
- resultGroupByCountries[element.nat] = array
- }else{
- resultGroupByCountries[element.nat] = [element]
- }
- })
- //console.log(JSON.stringify(resultGroupByCountries))
- console.log(resultGroupByCountries)
- /*
- Challenge 1.3 - write a function that returns a transformed array of profiles combined with photo data
- Start with this data
- */
- const profilesData = [
- {
- profile: { id: "26144385", some: "more", other: "misc" },
- photo_details: {
- photos: [{ small: "bar-1", medium: "baz-1" }]
- }
- },
- {
- profile: { id: "26144334", some: "even", other: "some more" },
- photo_details: {
- photos: [
- { small: "bar-2", medium: "baz-2" },
- { small: "fizz-2", medium: "buzz-2" }
- ]
- }
- }
- ];
- var resultNewData = []
- profilesData.map(function(element){
- var newElement = new Object()
- newElement['id'] = element.profile.id;
- newElement['some'] = element.profile.some;
- newElement['other'] = element.profile.other;
- var photosArray = element.photo_details.photos
- var newPhotosArray = new Array()
- photosArray.map(function(photo){
- var photoObject = new Object()
- photoObject['small'] = photo.small
- photoObject['medium'] = photo.medium
- newPhotosArray.push(photoObject)
- })
- newElement['photos'] = newPhotosArray
- resultNewData.push(newElement)
- })
- resultNewData.map(element => console.log(element))
- /* and then transform it into this shape:
- const expected = [
- {
- id: "26144385",
- some: "more",
- other: "misc",
- photos: [
- {
- small: "bar-1",
- medium: "baz-1"
- }
- ]
- },
- {
- id: "26144334",
- some: "even",
- other: "some more",
- photos: [
- {
- small: "bar-2",
- medium: "baz-2"
- },
- {
- small: "fizz-2",
- medium: "buzz-2"
- }
- ]
- }
- ];
- Note: This might feel super hard. Don't get stuck on it. Try it later
- */
Add Comment
Please, Sign In to add comment