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

时间:2019-02-14 03:57:19

标签: javascript arrays json object

我从对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”
  }
}

5 个答案:

答案 0 :(得分:6)

可以使用:

  1. 使用 Array.reduce name值累加到单个数组中。

  2. 使用 Object.keys Array.map 遍历键并将其映射到name数组。 / p>

  3. 使用 Object.values Array.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);

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