I like to know how i can get first specific value in a json like
$.each(data, function(i, val){
console.log(val.name);
});
sample output of above code is like this
John
Mary
Kite
but i like to get only first value like this
John
If data is an array, you can do
name=data[0].name
If it's an object, it's slightly more complicated
name=data[Object.keys(data)[0]].name;
Keep in mind that object keys aren't really sorted in any particular order
Object.keys approach won't guarantee any particular item as the "first" item. ... "...object keys aren't really sorted except for the order you put it in." No, there is no order. The order you give guarantees nothing.obj={foo:"bar",baz:"qux"}, this will give foo firstfor-in as well as Object.keys. Different browsers may behave differently. The same browser may even behave differently in different situations.If you return false from the iteration function, $.each() terminates the loop. So you can simply return false the first time:
$.each(data, function(i, val){
console.log(val.name);
return false;
});
Since object elements don't have any inherent order, there's no guarantee this will print a specific name. It will just pick one of the names arbitrarily.
data? Object or arrayJSON.parse()and pass it a "reviver" function. I think it'll give you the properties you encounter in order, though it'll traverse into a nested object before giving you the key for that object.JSON.parse()it, and use the property you found to grab the first.,