Javascript 查找 json 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19253753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript find json value
提问by Tropicalista
I need to search inside a json list of countries. The json is like:
我需要在 json 国家/地区列表中进行搜索。json 是这样的:
[
{"name": "Afghanistan", "code": "AF"},
{"name": "?land Islands", "code": "AX"},
{"name": "Albania", "code": "AL"},
{"name": "Algeria", "code": "DZ"}
]
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"
我只从数据库中获取代码并输出整个名称。所以如果我得到“AL”,我想从json“Albania”中检索
回答by showdev
I suggest using JavaScript's Array method filter()to identify an element by value. It filters data by using a "function to test each element of the array. Return true to keep the element, false otherwise.."
我建议使用 JavaScript 的 Array 方法filter()按值标识元素。它通过使用“函数来测试数组的每个元素来过滤数据。返回 true 以保留元素,否则返回 false..”
The following function filters the data, returning data for which the callback returns true, i.e. where data.codeequals the requested country code.
以下函数过滤数据,返回回调返回的数据true,即其中data.code等于请求的国家/地区代码。
function getCountryByCode(code) {
return data.filter(
function(data){ return data.code == code }
);
}
var found = getCountryByCode('DZ');
See the demonstration below:
请看下面的演示:
var data = [{
"name": "Afghanistan",
"code": "AF"
}, {
"name": "?land Islands",
"code": "AX"
}, {
"name": "Albania",
"code": "AL"
}, {
"name": "Algeria",
"code": "DZ"
}];
function getCountryByCode(code) {
return data.filter(
function(data) {
return data.code == code
}
);
}
var found = getCountryByCode('DZ');
document.getElementById('output').innerHTML = found[0].name;
<div id="output"></div>
回答by Brad Christie
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
}
}
回答by georg
First convert this structure to a "dictionary" object:
首先将此结构转换为“字典”对象:
dict = {}
json.forEach(function(x) {
dict[x.code] = x.name
})
and then simply
然后简单地
countryName = dict[countryCode]
For a list of countries this doesn't matter much, but for larger lists this method guarantees the instant lookup, while the naive searching will depend on the list size.
对于国家列表,这无关紧要,但对于较大的列表,此方法可保证即时查找,而简单搜索将取决于列表大小。
回答by Penny Liu
Just use the ES6 find()function in a functional way:
只需以函数方式使用 ES6 find()函数:
var data=[{name:"Afghanistan",code:"AF"},{name:"?land Islands",code:"AX"},{name:"Albania",code:"AL"},{name:"Algeria",code:"DZ"}];
let country = data.find(el => el.code === "AL");
// => {name: "Albania", code: "AL"}
console.log(country["name"]);
or Lodash _.find:
或 Lodash _.find:
var data=[{name:"Afghanistan",code:"AF"},{name:"?land Islands",code:"AX"},{name:"Albania",code:"AL"},{name:"Algeria",code:"DZ"}];
let country = _.find(data, ["code", "AL"]);
// => {name: "Albania", code: "AL"}
console.log(country["name"]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
回答by Osvaldo Cabrera
Making more general the showdevanswer.
使showdev答案更笼统。
var getObjectByValue = function (array, key, value) {
return array.filter(function (object) {
return object[key] === value;
});
};
Example:
例子:
getObjectByValue(data, "code", "DZ" );

