window.CardList = Backbone.Collection.extend(...);
var Cards = new CardList;
Cards.filter(...).pluck('values')
Is there a clean way to filter a collection and then pluck the values? The only work around I know is to reinitialize the collection:
new CardList(Cards.filter(...)).pluck('values')
OR to map the output after it's been filtered:
Cards.filter(...).map(...)
which seems weird since it has a perfectly good .pluck() method
CardList a backbone collection, once it is filtered or pluck'ed, it becomes an array of backbone models.
An array of backbone models cannot be pluck'ed again, unless you wrap it in another backbone collection (which is what the original post mentioned)
Alternative ways are:
Wrap it with underscore and chain it: _(Cards.filter(...)).chain().pluck('attributes').pluck('value').value()
Just map out the value (I ended up using this solution, it was the cleanest in the end):
_.map(Cards.filter(...), function(m) { return m.get('value') })
Cleaner still:
_.invoke(Cards.filter(...), 'get', 'value')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With