James Wayne James Wayne - 2 months ago 11
AngularJS Question

Angular loop through JSON with a limited number

Angular has a built-in feature to loop through JSON, for example I can do:

$scope.users = data.users


But I want to only loop through a number of users to enhance the performance like:

$scope.users = data.users[5] to data.users[10]


How would I be able to do this?

P.S. for pure javascript I can do:

for(var i = 5; i <= 11; i++) {
var user = data.users[i];
}


Of course this cannot add
user
to the scope.

Answer

You can use the slice method of an array.

$scope.users = data.users.slice(5, 11);