I have a Collection, that contains models, Each model has attributes, there is an attribute called country on each model. I want to iterate through the collection to get an array of countries. Each child has attributes,I am only interested in the country attribute. Tried _.pluck, _.each and _.where but unsuccessful. Any faster, quicker way to get a list?
Asked
Active
Viewed 70 times
-1
-
When you say using `pluck` was unsuccessful, how so? What exactly did you try and what was the result? – larz Apr 05 '17 at 15:13
-
var stooges = new Backbone.Collection([ {name: "Curly"}, {name: "Larry"}, {name: "Moe"} ]); var names = stooges.pluck("name"); Pluck only plucks if the property you are plucking is listed in the collection, I have a collection that lists models and and each model has attributes that has the property I am looking for. So not sure how to get that property using pluck – C_sharp Apr 05 '17 at 15:14
-
that looks correct to me. was the result of names `["Curly", "Larry", "Moe"]`? – larz Apr 05 '17 at 15:15
-
1Possible duplicate of [Backbone/Underscore: pluck a collection's attribute](http://stackoverflow.com/questions/7587383/backbone-underscore-pluck-a-collections-attribute) – Emile Bergeron Apr 05 '17 at 15:33
1 Answers
0
I found a variation of pluck on the backbone website. It lets you pluck an attribute from the models in the collections.
var stooges = new Backbone.Collection([
{name: "Curly"},
{name: "Larry"},
{name: "Moe"}
]);
var names = stooges.pluck("name");

Emile Bergeron
- 17,074
- 5
- 83
- 129

C_sharp
- 408
- 5
- 26