[javascript] 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 through the objects and print the desired data?

This is my result:

[
{
    "item1": {
        "sourceUuid": "5599ffac-4b99-47c7-9370-a25e7e465429",
        "targetUuid": "5599ffac-4b99-47c7-9370-a25e7effffff"
    }
},
{
    "item2": {
        "sourceUuid": "bf63fe50-8b2b-488d-b565-009fcaebdb45",
        "targetUuid": "-1"
    }
},
{
    "item3": {
        "sourceUuid": "0005fd96-f654-4781-8602-09fedc0cdd35",
        "targetUuid": "0005fd96-f654-4781-8602-09fedc0cdd35"
    }
}
]

This is what I want to print for each item (item1, item2, item3, ...):

Item Name: item1
Source: 5599ffac-4b99-47c7-9370-a25e7e465429
Target: 5599ffac-4b99-47c7-9370-a25e7effffff

So far I tried:

for (var i = 0, length = data.length; i < length; i++) {
for (obj in data[i]) {
    console.log(obj);

}
}

This only returns "item1", "item2" etc. But I don't know how access sourceUuid etc. from there

This question is related to javascript arrays json object

The answer is


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);
    }

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/


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor