Mehdi Bouhalassa
Mehdi Bouhalassa

Reputation: 195

JSON Parsing in Angular

I am trying to get a list of ID from a JSON file.

So far, the only way to access the "id" object is by using this:

console.log(photos.photosets.photoset[0].id);

As you might tell, it only gives me the correct ID of the first item.

If I try this, it gives me an "undefined":

console.log(photos.photosets.photoset.id);

Upvotes: 1

Views: 55

Answers (2)

dfsq
dfsq

Reputation: 193261

You will need to iterate array of photoset's and produce new array of ids from it. Array.prototype.map is convenient in this case:

var ids = photos.photosets.photoset.map(function(obj) {
    return obj.id;
});

Upvotes: 1

Dennis
Dennis

Reputation: 4017

No Angular, just JavaScript.

for (i = 0; i < photos.photosets.length; i++) {
 console.log(photos.photosets.photoset[i].id
}

Upvotes: 1

Related Questions