Andreas Köberle
Andreas Köberle

Reputation: 110972

How to iterate over a range of a Backbone collection?

How to iterate over a range, lets say from the 3. to 10., of a Backbone collection?

Upvotes: 4

Views: 1868

Answers (2)

Brendan Delumpa
Brendan Delumpa

Reputation: 1145

As opposed to slicing, you can be a bit more direct by using the collection's at method.

for (var idx=3;idx<=10;++idx) {
    var model = collection.at(idx);
    ...do something...
}

Upvotes: 1

nikoshr
nikoshr

Reputation: 33364

By slicing the array of models and using _.each on the result

var c=new Backbone.Collection(...);
_.each( c.models.slice(3,11), function(model) {
    console.log(model.get("id"));
});

slice is 0 based and the end index is excluded.

Upvotes: 5

Related Questions