"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 for
loop but I thought maybe there is a simpler way to do it.
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.
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.
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.
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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With