javascript 给定一个 JSON 对象,如何根据键查找项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5672788/
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
Given a JSON Object, How to find a item based on a Key?
提问by AnApprentice
Given a JSON object like:
给定一个 JSON 对象,如:
var data = {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
How can I do something like this:
我怎么能做这样的事情:
datagood = data.where(value == 55)
Is something like that possible with JS/jQuery? Thanks
JS/jQuery 可以实现类似的功能吗?谢谢
回答by David Tang
jQuery's grep
functionlets you filter through an array:
jQuery 的grep
函数可以让你过滤一个数组:
var datagood = $.grep(data.items, function (item) {
return item.value == 55;
});
If you want more powerful utilities, take a look at this SO question on JS LINQ libraries.
如果您想要更强大的实用程序,请查看有关 JS LINQ 库的这个 SO 问题。
Alternatively, underscore.jsis also useful for array/object manipulation.
或者,underscore.js也可用于数组/对象操作。
回答by Jonathan
回答by agershun
You can use Lo-Dashor Underscorelibraries _.where() and _.find() functions:
您可以使用罗短跑或下划线库_.where()和_.find()函数:
var res1 = _.where(data.items,{value:"55"});
var res2 = _.find(data.items,{value:"55"});
The difference between these two functions that _.where() returns all matched records, and _.find() - only first record.
这两个函数的区别在于 _.where() 返回所有匹配的记录,而 _.find() - 只返回第一条记录。
var data = {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"},
{value: "55", name: "Another Rudy Hamilton"}
]};
var res1 = _.where(data.items,{value:"55"});
var res2 = _.find(data.items,{value:"55"});
document.write('-.where(): ',JSON.stringify(res1),'<br>');
document.write('-.find(): ',JSON.stringify(res2),'<br>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
回答by James Kyburz
var data = {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
// using underscore.js http://documentcloud.github.com/underscore/
var t = _(data.items).find(function(x) {
return x.value == 55;
});
JSON.stringify(t) ; //# => {"value":"55","name":"Rudy Hamilton"}
// using pure javascript
var find = function(items, f) {
for (var i=0; i < items.length; i++) {
var item = items[i];
if (f(item)) return item;
};
}
t = find(data.items, function(x) {return x.value == "55";});
JSON.stringify(t) ; //# => {"value":"55","name":"Rudy Hamilton"}