Javascript在JSON对象中搜索 [英] Javascript search inside a JSON object
本文介绍了Javascript在JSON对象中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中有一个JSON字符串/对象。
I had a JSON string / object in my application.
{"list": [
{"name":"my Name","id":12,"type":"car owner"},
{"name":"my Name2","id":13,"type":"car owner2"},
{"name":"my Name4","id":14,"type":"car owner3"},
{"name":"my Name4","id":15,"type":"car owner5"}
]}
我的应用程序中有一个过滤器框,当我输入一个名称时那个方框,我们必须过滤对象并显示结果。
I had a filter box in my application, and when I type a name into that box, we have to filter the object and display the result.
例如,如果用户键入name并点击搜索,那么我们必须搜索全名在JSON对象中并返回数组,就像MySQL搜索...
For example, if the user types "name" and hits search, then we have to search full names in the JSON object and return the array, just like a MySQL search ...
我的问题是用字符串过滤json对象并返回数组....
My question is to filter the json object with string and return the array....
推荐答案
你可以循环遍历数组并找到匹配项:
You could just loop through the array and find the matches:
var results = [];
var searchField = "name";
var searchVal = "my Name";
for (var i=0 ; i < obj.list.length ; i++)
{
if (obj.list[i][searchField] == searchVal) {
results.push(obj.list[i]);
}
}
这篇关于Javascript在JSON对象中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文