从Firebase响应嵌套JSON对象获取键/值对

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Firebase响应嵌套JSON对象获取键/值对相关的知识,希望对你有一定的参考价值。

我从GET API调用Firebase数据库得到以下响应。它是嵌套的JSON对象。

我想使用javascript将每个嵌套对象的关键name的所有值都转换为数组

获取REST API响应:

{
  "barID1": {
    "address": "4 East Terrace, Sydney NSW 2000",
    "appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
    "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": "http://itunes.apple.com/app/idXXXXXXXXX",
     "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”
  }
}
答案

它可以使用:

  1. 使用Array.reducename值累积到单个数组中。
  2. 使用Object.keysArray.map迭代键并将其映射到name数组。
  3. 使用Object.valuesArray.map
  4. 使用Array.from并利用第二个映射函数参数将单个对象映射到names数组。

const obj = {"barID1":{"address":"4 East Terrace, Sydney NSW 2000","appStoreURL":"http://itunes.apple.com/app/idXXXXXXXXX","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":"http://itunes.apple.com/app/idXXXXXXXXX","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(ele.name)
}, []);
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) => ele.name);
console.log(name);

//using Array.from
name = Array.from(Object.values(obj), ele => ele.name);
console.log(name);
另一答案

您可以通过Object.values()提取输入对象的值,然后从每个map()值中获取name,如下所示,以实现此目的:

object
另一答案

你可以const data = { barID1: { address: "4 East Terrace, Sydney NSW 2000", appStoreURL: "http://itunes.apple.com/app/idXXXXXXXXX", 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: "http://itunes.apple.com/app/idXXXXXXXXX", 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" } } console.log( Object.values(data).map(object => object.name) ) json结构的map()

Object.values()
另一答案

最简单的解决方案是const json = { "barID1": { "address": "4 East Terrace, Sydney NSW 2000", "appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX", "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": "http://itunes.apple.com/app/idXXXXXXXXX", "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" } }; let res = Object.values(json).map(({name}) => name); console.log(res);

Object.values
另一答案

在这里,我使用const data = {"barID1":{"address":"4 East Terrace, Sydney NSW 2000","appStoreURL":"http://itunes.apple.com/app/idXXXXXXXXX","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":"http://itunes.apple.com/app/idXXXXXXXXX","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"}}; const namesArr = Object.values(data).map(({ name }) => name); console.log(namesArr);循环JSON中的每个对象,然后将名称推送到结果数组中。