Reputation: 131
For me i have a object like var object = [{"1":"w"},{"2":"z"}] ;
While iterating other array = '[{},{},{}]' i wanted to get object key and value i.e processing at index 0 of array should give me 1 and w respectively .
. While seeing some other posts of stack overflow i tried with ,
$.each(array, function(index, value) {
if(object[index] != undefined)
{
console.log("enterobject",$.parseJSON(JSON.stringify(object[index])));
console.log("enterobjectValue",$.parseJSON(JSON.stringify(object[index])).key);
console.log("enterobjectValue",$.parseJSON(JSON.stringify(object[index])).value);
}}
Only first console.log is printing like {"1":"w"} for index 0 , but not second and third log which i wanted to return me 1 and w respectively are not working .
Thanks
Upvotes: 2
Views: 47
Reputation: 91
It looks like you're getting back an array. If it's always going to consist of just one element, you could do this (yes, it's pretty much the same thing as Tomalak's answer):
$.each(result[0], function(key, value){
console.log(key, value);
});
If you might have more than one element and you'd like to iterate over them all, you could nest $.each():
$.each(result, function(key, value){
$.each(value, function(key, value){
console.log(key, value);
});
});
Upvotes: 1