How to filter JSON data using Javascript or jQuery?

这是我的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);

now I only want name and website data which is contain website is equal to "yahoo"

推荐答案

This is how you should do it : ( for google find)

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

Better solution : ( 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

Jquery相关问答推荐

这是否会导致内存泄漏?

如果链接包含多个关键字,jQuery 将 HTML 添加到链接

5 秒后关闭 jQuery 弹出模式

如何使用 jQuery 强制悬停状态?

与其他 Javascript 框架相比,为什么 jQuery 被如此广泛地采用?

使用 JavaScript/jQuery 重定向到 ASP.NET MVC 中的另一个页面

[本机代码]是什么意思?

jQuery $(document).ready () 触发两次

jQuery UI 对话框 - 关闭后不打开

延迟后运行功能

是否可以在 beforeunload 弹出窗口中显示自定义消息?

类方法中的Typescript this

jQuery: Select 所有具有自定义属性的元素

jQuery - 确定输入元素是文本框还是 Select 列表

将数据发布到 JsonP

在 javascript/浏览器中缓存 jquery ajax 响应

如何在 ReactJS 中使用 JQuery

如何让 jQuery 等到效果完成?

AJAX 成功中的 $(this) 不起作用

使用 jQuery 将一个标签替换为另一个标签