繁体   English   中英

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

[英]Get key/value pair from Firebase response nested JSON object

我从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()从每个object值的name ,如下所示,以实现此目的:

 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) ) 

您可以map() json结构的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); 

在这里,我for in循环JSON中的每个对象,然后将名称推送到结果数组中。

 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" } } let result = [] for (let i in data) { result.push(data[i].name) } console.log(result) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM