从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”
}
}
它可以使用:
- 使用
Array.reduce
将name
值累积到单个数组中。 - 使用
Object.keys
和Array.map
迭代键并将其映射到name
数组。 - 使用
Object.values
和Array.map
- 使用
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中的每个对象,然后将名称推送到结果数组中。
for in
以上是关于从Firebase响应嵌套JSON对象获取键/值对的主要内容,如果未能解决你的问题,请参考以下文章
Firebase Android - updateChildren 在嵌套 JSON 结构中写入键值
无法使用 Spark/Scala 从 JSON 嵌套键值对创建列和值
我正在尝试通过外部 api 从 json 数据中获取键值对并使用 angular 和 typescript 显示它。我怎样才能做到这一点?