Get key/value pair from Firebase response nested JSON object

  • A+

I am getting the following response from a GET API call to Firebase database. It is a nested JSON objects.

I want to get all the values for key name from each nested object into an array using JavaScript

GET REST API Response:

{   "barID1": {     "address": "4 East Terrace, Sydney NSW 2000",     "appStoreURL": "",     "description": “description text”,     "imgURLs": [ "Https:url1”,  "https:url2”, "https:url3” ],     "lat": -34.810585,     "lon": 138.616739,     "name": "Africola",     "phone": "(08) 8223 3885",     "status": "active",     "venueImgURL": "https:url”   },   "barID2": {     "address": "138/140 Gouger St, Sydney NSW 2000",     "appStoreURL": "",      "description": “description text”,     "imgURLs": [ "Https:url1”,  "https:url2”, "https:url3” ],     "lat": -34.848082,     "lon": 138.599813,     "name": "Disco Mexico Taqueria",     "phone": "0416 855 108",     "status": "active",     "venueImgURL": "https:url”   } } 


It can be done using :

  1. Using Array.reduce to accumulate the name values into a single array.

  2. Using Object.keys and to iterate through the keys and map it to the name array.

  3. Using Object.values and

  4. Using Array.from and utilizing the second mapping function parameter to map individual objects to an array of names.

const obj = {"barID1":{"address":"4 East Terrace, Sydney NSW 2000","appStoreURL":"","description":"description text","imgURLs":["Https:url1","https:url2","https:url3"],"lat":-34.810585,"lon":138.616739,"name":"Africola","phone":"(08) 8223 3885","status":"active","venueImgURL":"https:url"},"barID2":{"address":"138/140 Gouger St, Sydney NSW 2000","appStoreURL":"","description":"description text","imgURLs":["Https:url1","https:url2","https:url3"],"lat":-34.848082,"lon":138.599813,"name":"Disco Mexico Taqueria","phone":"0416 855 108","status":"active","venueImgURL":"https:url"}};  //using Object.values & reduce let name = Object.values(obj).reduce((acc, ele) =>{  return acc.concat( }, []); console.log(name);  //using Object.keys & map  name = Object.keys(obj).map((ele) => obj[ele]['name']); console.log(name);  //using Object.values & map name = Object.values(obj).map((ele) =>; console.log(name);  //using Array.from name = Array.from(Object.values(obj), ele =>; console.log(name);


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: