I have Backbone.js collection that holds (for example) 30 items.
I want to pass to my template filtered collection consist of every 3rd item in the original collection.
Does anyone know how it can be done elegantly? CoffeeScript code is preferred.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming here that
originalCollectionis your existing collectionThis code works by looping through each existing model, and only adding the model the new collection if it’s index is a multiple of 3.
You could make this a little nicer, by using the underscore
eachmethod exposed by Underscore.js in Backbone Collections:Converting the above to CoffeeScript results in: