Skip to content Skip to sidebar Skip to footer

Accessing Objects In Json Array (javascript)

Possible Duplicate: I have a nested data structure / JSON, how can I access a specific value? I have a service that returns nested Objects in a JSON Array. How can I loop throug

Solution 1:

You can loop the array with a for loop and the object properties with for-in loops.

for (var i=0; i<result.length; i++)
    for (var name in result[i]) {
        console.log("Item name: "+name);
        console.log("Source: "+result[i][name].sourceUuid);
        console.log("Target: "+result[i][name].targetUuid);
    }

Solution 2:

Use a loop

for(var i = 0; i < obj.length; ++i){
   //do something with obj[i]for(var ind in obj[i]) {
        console.log(ind);
        for(var vals in obj[i][ind]){
            console.log(vals, obj[i][ind][vals]);
        }
   }
}

Demo: http://jsfiddle.net/maniator/pngmL/

Post a Comment for "Accessing Objects In Json Array (javascript)"