Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
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. –  Zack Argyle Dec 4 '13 at 23:00

2 Answers 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

share|improve this answer

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;
            }
        });

    });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.