44

I have the following JSON and I want to keep the array in the same format but only include objects with type "ar". How can I filter this JSON with JavaScript to only include the types I need?

[{"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"},
]
brasofilo
  • 25,496
  • 15
  • 91
  • 179
JordanBarber
  • 2,041
  • 5
  • 34
  • 62
  • 7
    Important technical distinction: *JSON is a string* (it is a way to serialize JavaScript data structure as string data, nothing more). The only thing you can do with it is either perform string operations, or *parse* it into JavaScript data structures. What you are asking about is filtering an array of objects, based on some key used by those objects; you are **not** asking about filtering JSON. – Mike 'Pomax' Kamermans Feb 09 '17 at 17:26
  • Possible duplicate of [How to filter JSON Data in JavaScript or jQuery?](https://stackoverflow.com/questions/23720988/how-to-filter-json-data-in-javascript-or-jquery) – probitaille Oct 04 '18 at 19:29

4 Answers4

70

You should use filter method.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Provided function is a callback which is applied to each element of the array.

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:

var filtered = arr.filter(a => a.type == "ar");
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
4

Using filter method you can filter the array to return only those elements which match a particular condition data.filter((x)=>x.type === "ar");

The filter method creates a new array with all elements that will pass the condition 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);
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
2

You could use filter function. 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);
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
0

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);
user2085143
  • 4,162
  • 7
  • 39
  • 68