如何使用JavaScript或jQuery过滤JSON数据? - 代码日志

如何使用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”

你的json是无效的

这是你应该怎么做的((谷歌发现)

$([
  {"name":"Lenovo Thinkpad 41A4298","website":"google222"},
  {"name":"Lenovo Thinkpad 41A2222","website":"google"}
  ])
    .filter(function (i,n){
        return n.website==='google';
    });

更好的解决方案:(Salman’s)

$.grep( [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"}], function( n, i ) {
  return n.website==='google';
});

http://jsbin.com/yakubixi/4/edit

http://stackoverflow.com/questions/23720988/how-to-filter-json-data-in-javascript-or-jquery

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:如何使用JavaScript或jQuery过滤JSON数据?