简体   繁体   English

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

[英]How to filter JSON Data in JavaScript or jQuery?

How to filter JSON data using Javascript or jQuery? 如何使用Javascript或jQuery过滤JSON数据?

This my JSON data: 这是我的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: 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 http://jsbin.com/yakubixi/4/edit

No need for jQuery unless you target old browsers and don't want to use shims. 除非您针对旧浏览器并且不想使用填充程序,否则不需要jQuery。

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

In ES2015: 在ES2015中:

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

Try this way, allow you even filter by other key 试试这种方式,让您甚至可以通过其他键进行过滤

data: 数据:

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

usage: 用法:

//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'});

filter function 过滤功能

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];
    });
  });

}

The following code works for me: 以下代码适用于我:

 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) 

You can use jQuery each function as it is explained below: 你可以使用jQuery的每个函数 ,如下所述:

Define your data: 定义您的数据:

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"}]';

Parse JSON string to JSON object: 将JSON字符串解析为JSON对象:

var json = JSON.parse(jsonStr);

Iterate and filter: 迭代和过滤:

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

The values can be retrieved during the parsing: 可以在解析期间检索值:

 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 ) 

I know the question explicitly says JS or jQuery, but anyway using lodash is always on the table for other searchers I suppose. 我知道问题明确地说JS或jQuery,但无论如何使用lodash总是在我认为的其他搜索者的桌子上。

From the source docs : 来自源文档

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']

So the solution for the original question would be just one liner: 因此,原始问题的解决方案只是一个班轮:

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

It iterates through the json objects, and searches each value you are concerned about, 'website', and if it equals "yahoo" you can then return that value or do whatever you like there. 它遍历json对象,并搜索您关注的每个值,'website',如果它等于“yahoo”,您可以返回该值或在那里做任何你喜欢的事情。 Right now it just logs that element to the console. 现在它只是将该元素记录到控制台。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM