Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to search in JSON by attribute?

"names": [
    {
        "id": 17,
        "user_id": 9,
        "code": "de",
        "name": "Ich bin Hans",
        "created_at": "2017-07-31 12:43:19",
        "updated_at": "2017-07-31 12:43:19"
    },
    {
        "id": 18,
        "user_id": 9,
        "code": "en",
        "name": "My name is Hans",
        "created_at": "2017-07-31 12:43:19",
        "updated_at": "2017-07-31 12:43:19"
    }
]

Is there any method to get JSON object with code='en' from the above JSON array in jQuery?

I could do it with forloop but I thought maybe there is a simpler way to do it.

like image 901
Nevermore Avatar asked Aug 04 '17 08:08

Nevermore


People also ask

What is the use of the search function in JSON?

The search function will search for a value that is exactly equal inside a json object. You can use it to search for each element of an array for example, just adjust the code. A stack was necessary, since we need to keep track of the parents.

What is an example of a JSON object?

Example. { "name":"John", "age":30, "car":null } JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon.

What are keys and values in a JSON object?

Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma. Values in a JSON object can be another JSON object.

How do you write a string in JSON?

In JSON, string values must be written with double quotes: Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript. You will learn how to convert JavaScript objects into JSON later in this tutorial.


2 Answers

Use Array.prototype.filter function in vanilla JS to filter out the object - see demo below:

var obj={names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};

var result = obj.names.filter(function(e){return e.code == 'en'})
console.log(result);

ES6 version is even simpler:

var obj={names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};

var result = obj.names.filter(e => e.code == 'en');
console.log(result);
like image 111
kukkuz Avatar answered Sep 19 '22 01:09

kukkuz


Use Jquery grep function. Actually you are not searching object, you are searching though an array of objects (names).

Finds the elements of an array which satisfy a filter function. The original array is not affected.

 var input = {names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};
 
var result = $.grep(input.names, function(obj) {
    return obj.code === "en";
});
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Another is Array#find function

var input = {names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};
 
var result = input.names.find(item => {
   return item.code == 'en'
})
console.log(result);
like image 28
Suresh Atta Avatar answered Sep 18 '22 01:09

Suresh Atta