Christian Giupponi Christian Giupponi - 26 days ago 19
Javascript Question

Access JSON data - undefined

I have this json:

{
"total": 1,
"per_page": "10",
"current_page": 1,
"last_page": 1,
"next_page_url": null,
"prev_page_url": null,
"from": 1,
"to": 1,
"data": [
{
"id": 2,
"name": "Artista di prova",
"artist_hash": "Gqrrx6NrtqqMpvEN",
"spotify_followers": null,
"spotify_popularity": 66,
"image_small": null,
"image_large": null,
"fully_scraped": 0,
"updated_at": "2016-11-06 08:21:02",
"bio": "",
"wiki_image_large": null,
"wiki_image_small": null,
"tracks": [
{
"id": 1,
"name": "Traccia di prova",
"album_name": "Album di prova",
"number": 1,
"duration": 18133,
"artists": [
"Artista di prova"
],
"youtube_id": null,
"spotify_popularity": 50,
"album_id": 1,
"temp_id": null,
"url": "http://myapp.app/track/1/mov/stream",
"artist_id": 2
},
{
"id": 3,
"name": "Traccia di prova 2",
"album_name": "Album di prova",
"number": 1,
"duration": 18133,
"artists": [
"Artista di prova"
],
"youtube_id": null,
"spotify_popularity": 50,
"album_id": 1,
"temp_id": null,
"url": "http://myapp.app/track/1/mov/stream",
"artist_id": 2
}
]
}
]


}

I need to access to the
tracks
field and get all the data.

I tried with:

$http.get('artist', {params:params}).success(function(response) {
$scope.items = response.data;
$scope.tracks = response.data.tracks;

console.log($scope.tracks);


})
};


But I get
undefined
.

How can I access to
tracks
field?

Answer

data property of response is an array, so you need to access it by index:

response.data[0].tracks;
Comments