0

If I am getting data from a json api (playlists.json), and the playlist object has a user_id data field, how can I get a specific playlist with user_id == 1?

I tried doing:

var playlists = Restangular.all('playlists');

playlists.getList().then(function(data) {
    for(var i = 0; i < data.length; i++) {
        var obj = data[i];
        if(obj.user_id == 1) {
            $scope.playlist = obj;                
        }
    }
});

But I would rather not retrieve all the playlists and loop through all of them as it seems very inefficient.

Thanks in advance!

1
  • 1
    Whether you do it, or let a framework do it, there will be a loop happening. Unless you can make a separate json file. Commented Dec 4, 2013 at 23:00

2 Answers 2

2

If json api represents collection of playlist you can use:

Restangular.one('user_id', 1).then(function(data) {   
    $scope.playlist = data;  
}); 

But if you want preloaded list, I think there is no way but use loop

Sign up to request clarification or add additional context in comments.

Comments

0

you could try something like this:

playlists.getList().then(function(data) {
        angular.forEach(data, function(value, key) {
            if(value.user_id == 1) {
                scope.playlist = value;
                return;
            }
        });

    });

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.