I have a json like:
var json = [{"Google":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Linkedin":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Uber":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Akamai":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Apple":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
{"Hulu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"IBM":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Spotify":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Amazon":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];
I want to iterate through it. json.length returns 9 => implying there are 9 key/value pairs in this json. How can I find key at index 0 and its corresponding value in javascript.
I tried using something like:
$.each(json, function(k, v) {
//display the key and value pair
alert(k + ' is ' + v);
});
but here key is returning 1,2,3... and value as [object object]
I have a json like:
var json = [{"Google":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Linkedin":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Uber":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Akamai":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Apple":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
{"Hulu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"IBM":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Spotify":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Amazon":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];
I want to iterate through it. json.length returns 9 => implying there are 9 key/value pairs in this json. How can I find key at index 0 and its corresponding value in javascript.
I tried using something like:
$.each(json, function(k, v) {
//display the key and value pair
alert(k + ' is ' + v);
});
but here key is returning 1,2,3... and value as [object object]
Share Improve this question asked Apr 28, 2016 at 4:53 BharthanBharthan 1,5322 gold badges17 silver badges30 bronze badges 1- Your json var is an array that contains json object. – user3502626 Commented Apr 28, 2016 at 4:56
6 Answers
Reset to default 3Use
Array.prototype.forEach()
to iterate through thearray
andObject.keys
over iterated item to get all thekeys
of individual object.
var json = [{
"Google": [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"Linkedin": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"Uber": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"Akamai": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"Apple": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
}, {
"Hulu": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"IBM": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"Spotify": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, {
"Amazon": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}];
json.forEach(function(item) {
var keys = Object.keys(item);
console.log(keys);
//to get the values
keys.forEach(function(key) {
console.log(item[key]);
})
});
How can I find key at index 0 and its corresponding value in javascript.
You can get all the key names in an object by using Object.keys
method.
try
var key0 = Object.keys(json[0])[0];
this will give you key name at index 0.
And
value0 = json[0][key0];
This can be made more generic to iterate through entire json
json.forEach(function(val){
var key = Object.keys(val)[0];
var value = val[key];
console.log( "key is " + key );
console.log( "value is " + value );
});
You should consider re-formatting your JSON data.
I personally think it's good practice to give each value an appropriate "attribute" when using JSON Objects (Giving "Google" an attribute of "pany") for easier access to the values & readability.
EG:
var json = [{'pany':"Google",'values':[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"Linkedin",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"Uber",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"Akamai",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"Apple",'values':[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
{'pany':"Hulu",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"IBM",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"Spotify",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{'pany':"Amazon",'values':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];
Then, you can iterate through the values by using 2 $.each loops. One nested after the other.
$.each(json,function(key,val){
var pany = val.pany;
console.log('Below values belong to '+pany);
$.each(val.values,function(k,v){
var value = v;
console.log(value);
});
console.log('End of values belonging to '+pany);
});
To find the key (I assume by key you mean the pany name) at index 0, you can access the json array with the index of 0 as such.
var pany = json[0].pany;
var valueArray = json[0].values;
something like this
var json = [{"Google":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Linkedin":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Uber":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Akamai":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Apple":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
{"Hulu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"IBM":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Spotify":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Amazon":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}];
json.forEach(function(n){
Object.keys(n).forEach(function(x) {
document.writeln(x + ' is ' + n[x] + '</br>');
})
}) ;
Hope this will be useful
var json = [
{"Google":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Linkedin":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Uber":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Akamai":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Apple":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]},
{"Hulu":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"IBM":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Spotify":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
{"Amazon":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
];
json.forEach(function(key,value){
var _getCurrentItem = key; // will give google:Array[18]....
// Loop through this object to get its key & value
for(var keys in _getCurrentItem){
console.log('Key is ' + keys +' & its value' +_getCurrentItem[keys]);
})
Check this Jsfiddle for working model
[] represents an array which means the keys are integers : 0 to (N-1) : N is the number of items in the array/list. i.e. : var t = ["Me", "You", "Them"];
where t[0] is "Me"
and t[2] is "Them"
.
{} represents an object or associative array : an array with string keys. i.e. var t = {"firstperson" : "me", "secondperson" : "you", "thirdperson" : "them"};
where t["firstperson"] is "me"
.
So, in your data representation, you should have two loops, one within the other :
$.each(json, function(k, v) {
//display the key and value pair
$.each(v, function(k2, v2) {
//display the key and value pair
alert('json[' + k + '][' + k2 + '] = ' + v2);
});
});