0

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?

1
  • use response.data[0].tracks Commented Nov 6, 2016 at 9:43

2 Answers 2

1

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

response.data[0].tracks;
Sign up to request clarification or add additional context in comments.

Comments

0

Remember that tracks is an array of objects and also, remember that it sits under the data key which is also an array. The naive way will be to get each data with their respective index, as such: response.data[0].tracks[anotherIndex] to get one object in the array and response.data[0].tracks to log the array. In a real world scenario though, you may want to properly loop over the data and tracks and render their values on your component.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.