问题描述
如果我在下面的嵌套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”
}
}
1楼
Bibberty
2
2019-02-21 03:29:31
在这里,您的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'));
2楼
Shidersz
1
已采纳
2019-02-21 03:30:32
一种解决方案是将与如下一个示例所示:
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);
3楼
Brad
1
2019-02-21 03:30:32
我将为此使用和 。
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);
小提琴: :