按键值

时间:2017-02-09 17:21:54

标签: javascript arrays json object filter

我有以下JSON,我希望将数组保持为相同的格式,但只包含类型为"ar"的对象。如何使用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"},
]

4 个答案:

答案 0 :(得分:14)

您应该使用filter方法。

filter()方法创建一个新的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"},
];
var filtered=arr.filter(function(item){
    return item.type=="ar";         
});
console.log(filtered);




此外,您可以使用shorter方式:

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

答案 1 :(得分:2)

您可以使用filter功能。首先检查browser support

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

希望这有帮助。

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

答案 2 :(得分:1)

使用filter方法,您可以过滤数组,只返回符合特定条件的元素data.filter((x)=>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);

答案 3 :(得分:0)

不像上面的答案一样干净,但仍然可以完成这项工作

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