Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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". 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"}, ] 
like image 549
JordanBarber Avatar asked Feb 09 '17 17:02

JordanBarber


People also ask

How to get value for key from nested JSON object in JavaScript?

Use the array filter function to get the JSON value for the key in JavaScript. Simple example code gets value for a key from nested JSON object in JavaScript.

How to filter an object by key-value in Python?

To filter an object by key-value, you can iterate over the object using Object.entries () Object.entries () returns a 2 dimensional array of the key-value pairs. Each element in the array has 2 elements: the first is the key, and the 2nd is the value.

How to filter any table in the database using JSON?

Since we need to be able to filter any table in the database, we should first get the columns that exist in the supplied table. Extract the keys and values in tabular format from the JSON param string. If the supplied JSON has some invalid columns, it’s safer to validate the JSON and raise an exception early with a clear error message.

Why do we need to keep filtering in JSON params?

And usually, the data that is presented to the user needs to be filtered in specific ways based on what the user wants to see. So, keeping the filtering logic in JSON params might be an option as this gets rid of all the query building logic from the backend.


2 Answers

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"); 
like image 100
Mihai Alexandru-Ionut Avatar answered Oct 08 '22 16:10

Mihai Alexandru-Ionut


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);
like image 35
Abhinav Galodha Avatar answered Oct 08 '22 17:10

Abhinav Galodha