如何在 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);

现在我只想名称和网站数据是包含网站是等于 “雅虎”

414923 次浏览

你应该这样做: (为谷歌查找)

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

解决方案: (萨尔曼)

$.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,除非你的目标是老的浏览器,并且不想使用垫片。

var yahooOnly = JSON.parse(jsondata).filter(function (entry) {
return entry.website === 'yahoo';
});

在 ES2015年:

const yahooOnly = JSON.parse(jsondata).filter(({website}) => website === 'yahoo');

你可使用 查询每个函数,详情如下:

定义你的数据:

var jsonStr = '[{"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"}]';

将 JSON 字符串解析为 JSON 对象:

var json = JSON.parse(jsonStr);

迭代和过滤:

$.each(JSON.parse(json), function (idx, obj) {
if (obj.website == 'yahoo') {
// do whatever you want
}
});

尝试这种方式,甚至允许您过滤其他关键

数据:

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 JSON
var my_json = JSON.stringify(my_data)
//We can use {'name': 'Lenovo Thinkpad 41A429ff8'} as criteria too
var 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];
});
});


}

我知道这个问题明确指出是 JS 还是 jQuery,但无论如何使用 我想,对于其他搜索者来说,loash 总是在桌面上。

来源:

var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred',   'age': 40, 'active': false }
];


_.filter(users, function(o) { return !o.active; });
// => objects for ['fred']


// The `_.matches` iteratee shorthand.
_.filter(users, { 'age': 36, 'active': true });
// => objects for ['barney']


// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']


// The `_.property` iteratee shorthand.
_.filter(users, 'active');
// => objects for ['barney']

因此,原始问题的解决方案只有一个:

var result = _.filter(data, ['website', 'yahoo']);

它遍历 json 对象,并搜索您关心的每个值,“网站”,如果它等于“ yahoo”,那么您可以返回该值,或者在那里做任何您喜欢的事情。现在它只是将该元素记录到控制台。

jsonObj.forEach(function (element, index) {
if(element['website'] === 'yahoo'){
console.log('found', element)
}
})

可以在解析过程中检索这些值:

var yahoo = [], j = `[{"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"}]`


var data = JSON.parse(j, function(key, value) {
if ( value.website === "yahoo" ) yahoo.push(value);
return value; })


console.log( yahoo )

以下代码适用于我:

var 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"}]


var data_filter = data.filter( element => element.website =="yahoo")
console.log(data_filter)