当前位置: 代码迷 >> JavaScript >> 使用JavaScript从嵌套JSON对象中的值获取上层密钥?
  详细解决方案

使用JavaScript从嵌套JSON对象中的值获取上层密钥?

热度:51   发布时间:2023-06-05 09:23:29.0

如果我在下面的嵌套JSON中知道密钥name的值Africola ,那么如何在JavaScript中获取其对应的上级密钥' barID1 '?

{
  "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”
  }
}

在这里,您的JSON格式错误,包含无效字符。 在按键上使用查找

 let 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" } }; let result = Object.keys(obj).find(key => obj[key].name === "Africola"); console.log(result); 

作为功??能:

 let 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" } }; const findKeyByName = (obj, search) => Object.keys(obj).find(key => obj[key].name === search); console.log(findKeyByName(obj, 'Africola')); console.log(findKeyByName(obj,'Disco Mexico Taqueria')); 

一种解决方案是将与如下一个示例所示:

 const input = { "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 [key, val] = Object.entries(input).find( ([k, v]) => v.name === "Africola" ); console.log("key is :", key); console.log("value is :", val); 

我将为此使用和 。

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 [key, obj] = Object.entries(data).find(([key, obj]) => {
  return obj.name === 'Africola';
});

console.log(key, obj);

小提琴: :