jQuery:如何通过ID等从JSON数据列表中获取JSON数据行? [英] jquery: how to get a row of json data from the json data list by id, etc?
本文介绍了jQuery:如何通过ID等从JSON数据列表中获取JSON数据行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我下面有一个json数据,如何通过 id 或电子邮件等选择数据行?
I have a json data like this below, how can I select the row of data by its id or by its email, etc?
[
Object { id="40", full_name="lau", email="kok@com", more...},
Object { id="39", full_name="Kok", email="lau@lauthiamkok.net", more...},
Object { id="5", full_name="Alice", email="alice@umbrellacorp.com", more...},
4 more...
]
如果我这样做
console.log(app.contacts[0]);
我总是只获得第一行.
我可以使用jquery.get()
从列表中获取一行,如下所示吗?
Can I use jquery.get()
to get a row from the list, something like this below?
console.log(app.contacts.get({id:5}));
推荐答案
您必须迭代并检查循环中的id或email属性
You have to iterate and check the id or email property in the loop
var obj;
for (var i=0; i<arr.length; i++) {
if ( arr[i].id == '40' ) {
obj = arr[i];
break;
}
}
此外,发布的数组/对象无效
Also, the posted array / objects are not valid
这篇关于jQuery:如何通过ID等从JSON数据列表中获取JSON数据行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文