如何在JavaScript或jQuery中过滤JSON数据?

如何使用Javascript或jQuery过滤JSON数据?


这是我的JSON数据:


[{"name":"Lenovo Thinkpad 41A4298","website":"google"},

{"name":"Lenovo Thinkpad 41A2222","website":"google"},

{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},

{"name":"Lenovo Thinkpad 41A424448","website":"google"},

{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},

{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},

{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},

{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]

JavaScript:


obj1 = JSON.parse(jsondata);

现在我只想要包含网站的名称和网站数据等于“ yahoo”


牧羊人nacy
浏览 1055回答 3
3回答

慕勒3428872

尝试这种方式,甚至可以通过其他键进行过滤数据:var my_data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"},{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},{"name":"Lenovo Thinkpad 41A424448","website":"google"},{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];用法://We do that to ensure to get a correct JSONvar my_json = JSON.stringify(my_data)//We can use {'name': 'Lenovo Thinkpad 41A429ff8'} as criteria toovar filtered_json = find_in_object(JSON.parse(my_json), {website: 'yahoo'});过滤功能function find_in_object(my_object, my_criteria){  return my_object.filter(function(obj) {    return Object.keys(my_criteria).every(function(c) {      return obj[c] == my_criteria[c];    });  });}
打开App,查看更多内容
随时随地看视频慕课网APP