Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  16] [ 7]  / answers: 1 / hits: 34360  / 12 Years ago, mon, march 19, 2012, 12:00:00

I have a JSON array like below:



var jsonArray = [{k1:v1},{k2:v2},{k3:v3},{k4:v4},{k5:v5}]


I don't know which keys does exists in this array.
I want to get all the existing key from the array.



It should be possible something like this:



for(i=0;i<jsonArray.lenght;i++){
// something like- key = jsonArray[i].key
// alert(key);
}


Please tell me the method or way to get all keys existing in Json array.



Regards


More From » jquery

 Answers
9

Try this:



var L = jsonArray.length;
for (var i = 0; i < L; i++) {
var obj = jsonArray[i];
for (var j in obj) {
alert(j);
}
}


I've also made some modifications of your current code (like length caching).


[#86753] Saturday, March 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
milor

Total Points: 284
Total Questions: 93
Total Answers: 115

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;