Javascript找到json值 [英] Javascript find json value
本文介绍了Javascript找到json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在json国家/地区列表中搜索。 json就像:
I need to search inside a json list of countries. The json is like:
[
{"name": "Afghanistan", "code": "AF"},
{"name": "Åland Islands", "code": "AX"},
{"name": "Albania", "code": "AL"},
{"name": "Algeria", "code": "DZ"}
]
<我只从数据库中获取代码并输出整个名称。因此,如果我得到AL,我想从json阿尔巴尼亚中检索
I get from database only the code and would output the entire name. So if I get "AL" I would like to retrieve from json "Albania"
推荐答案
var obj = [
{"name": "Afghanistan", "code": "AF"},
{"name": "Åland Islands", "code": "AX"},
{"name": "Albania", "code": "AL"},
{"name": "Algeria", "code": "DZ"}
];
// the code you're looking for
var needle = 'AL';
// iterate over each element in the array
for (var i = 0; i < obj.length; i++){
// look for the entry with a matching `code` value
if (obj[i].code == needle){
// we found it
// obj[i].name is the matched result
}
}
这篇关于Javascript找到json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文