简体   繁体   English

按键值过滤 JSON

[英]Filter JSON by key value

I have the following JSON and I want to keep the array in the same format but only include objects with type "ar" .我有以下 JSON,我想保持数组的格式相同,但只包含类型为"ar"的对象。 How can I filter this JSON with JavaScript to only include the types I need?如何使用 JavaScript 过滤此 JSON 以仅包含我需要的类型?

[{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]

You should use filter method.您应该使用filter方法。

The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新array ,其中包含通过提供的函数实现的测试的所有元素。

Provided function is a callback which is applied to each element of the array.提供的函数是应用于数组每个元素的callback

 var arr = [{"time":"2016-07-26 09:02:27","type":"aa"}, {"time":"2016-04-21 20:35:07","type":"ae"}, {"time":"2016-08-20 03:31:57","type":"ar"}, {"time":"2017-01-19 22:58:06","type":"ae"}, {"time":"2016-08-28 10:19:27","type":"ae"}, {"time":"2016-12-06 10:36:22","type":"ar"}, {"time":"2016-07-09 12:14:03","type":"ar"}, {"time":"2016-10-25 05:05:37","type":"ae"}, {"time":"2016-06-05 07:57:18","type":"ae"}, {"time":"2016-10-08 22:03:03","type":"aa"}, {"time":"2016-08-13 21:27:37","type":"ae"}, {"time":"2016-04-09 07:36:16","type":"ar"}, {"time":"2016-12-30 17:20:08","type":"aa"}, {"time":"2016-03-11 17:31:46","type":"aa"}, {"time":"2016-05-04 14:08:25","type":"ar"}, {"time":"2016-11-29 05:21:02","type":"ar"}, {"time":"2016-03-08 05:46:01","type":"ar"}, ]; console.log(arr.filter(function(item){ return item.type == "ar"; }));

Also, you can use a shorter way with arrow functions :此外,您可以使用arrow功能shorter方式:

var filtered = arr.filter(a => a.type == "ar");

Using filter method you can filter the array to return only those elements which match a particular condition data.filter((x)=>x.type === "ar");使用filter方法,您可以过滤数组以仅返回与特定条件匹配的元素data.filter((x)=>x.type === "ar");

The filter method creates a new array with all elements that will pass the condition x.type === "ar" filter方法创建一个新数组,其中包含将通过条件x.type === "ar"的所有元素

 var data =[{"time":"2016-07-26 09:02:27","type":"aa"}, {"time":"2016-04-21 20:35:07","type":"ae"}, {"time":"2016-08-20 03:31:57","type":"ar"}, {"time":"2017-01-19 22:58:06","type":"ae"}, {"time":"2016-08-28 10:19:27","type":"ae"}, {"time":"2016-12-06 10:36:22","type":"ar"}, {"time":"2016-07-09 12:14:03","type":"ar"}, {"time":"2016-10-25 05:05:37","type":"ae"}, {"time":"2016-06-05 07:57:18","type":"ae"}, {"time":"2016-10-08 22:03:03","type":"aa"}, {"time":"2016-08-13 21:27:37","type":"ae"}, {"time":"2016-04-09 07:36:16","type":"ar"}, {"time":"2016-12-30 17:20:08","type":"aa"}, {"time":"2016-03-11 17:31:46","type":"aa"}, {"time":"2016-05-04 14:08:25","type":"ar"}, {"time":"2016-11-29 05:21:02","type":"ar"}, {"time":"2016-03-08 05:46:01","type":"ar"}, ]; var result = data.filter((x)=>x.type === "ar"); console.log(result);

You could use filter function.你可以使用filter功能。 Check browser support first:首先检查浏览器支持

var result = a.filter(function(v){
    return v.type==='ar';
})

Hope this helps.希望这可以帮助。

 var list = [ {"time":"2016-07-26 09:02:27","type":"aa"}, {"time":"2016-04-21 20:35:07","type":"ae"}, {"time":"2016-08-20 03:31:57","type":"ar"}, {"time":"2017-01-19 22:58:06","type":"ae"}, {"time":"2016-08-28 10:19:27","type":"ae"}, {"time":"2016-12-06 10:36:22","type":"ar"}, {"time":"2016-07-09 12:14:03","type":"ar"}, {"time":"2016-10-25 05:05:37","type":"ae"}, {"time":"2016-06-05 07:57:18","type":"ae"}, {"time":"2016-10-08 22:03:03","type":"aa"}, {"time":"2016-08-13 21:27:37","type":"ae"}, {"time":"2016-04-09 07:36:16","type":"ar"}, {"time":"2016-12-30 17:20:08","type":"aa"}, {"time":"2016-03-11 17:31:46","type":"aa"}, {"time":"2016-05-04 14:08:25","type":"ar"}, {"time":"2016-11-29 05:21:02","type":"ar"}, {"time":"2016-03-08 05:4601","type":"ar"}, ]; var result = list.filter(function(obj, index){ return obj.type==='ar'; }) console.log(result);

Not as clean as the answer above, but will still do the job不像上面的答案那么干净,但仍然可以完成工作

var list = [{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]

var newList = [];

for (var i = 0; i < list.length; i++) {
    if (list[i].type === 'ar') {
        newList.push(list[i]);
    }
}

console.log(newList);

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

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