Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Objects in JSON Array (JavaScript) [duplicate]

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

like image 445
maze Avatar asked Jan 08 '13 15:01

maze


People also ask

How do you find duplicate objects in an array?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.

Is duplicate key allowed in JSON?

I've done some research and I understood that "duplicate" keys in JSON are legal, but different parsers act differently in handling this.

Does JavaScript array allow duplicates?

Using Array.If any item in the array, both indices don't match, you can say that the array contains duplicates. The following code example shows how to implement this using JavaScript some() method, along with indexOf() and lastIndexOf() method. Output: Duplicate elements found.

How do you remove duplicate objects from an array of objects?

To remove the duplicates from an array of objects:Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.


2 Answers

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);
    }
like image 84
Bergi Avatar answered Nov 02 '22 19:11

Bergi


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/

like image 38
Naftali Avatar answered Nov 02 '22 21:11

Naftali