我从对Firebase数据库的GET API调用中收到以下响应。它是一个嵌套的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”
}
}
答案 0 :(得分:6)
可以使用:
使用 Array.reduce
将name
值累加到单个数组中。
使用 Object.keys
和 Array.map
遍历键并将其映射到name
数组。 / p>
使用 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);
答案 1 :(得分:2)
您可以通过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) )
答案 2 :(得分:2)
您可以map() Object.values()的json结构。
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);
答案 3 :(得分:2)
最简单的解决方案是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);
答案 4 :(得分:1)
在这里,我使用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)