How to pluck a Backbone collection's attribute

我是研究僧i 提交于 2019-12-03 15:48:48

问题


I want to create an array of specific attribute values from a Backbone collection.

var days = _.select(
    this.collection.models,
    function(model) {
        return model.attributes.type === 'session';
    }
);

days = _.pluck(days, 'attributes'),
days = _.pluck(days, 'date');

This works, but seems inefficient. Is there a way to accomplish the same thing without having to define days three times?


回答1:


pluck is a convenience method that wraps map, and map is available directly on the collection, which should make this easier.

assuming you are trying to get the date attribute out of your models, you can do this:

days = this.collection.map(function(model){
  return model.get('date');
});

your select call is also available on the collection directly, as the filter method.

days = this.collection.filter(function(model){ 
  return model.attributes.type === 'session'; 
});

you can chain these two together, but it helps if you define methods separately:

var sessionFilter = function(model){
  return model.attributes.type === 'session'; 
};
var getDate = function(model){ return model.get('date'); }

days = this.collection.filter(sessionFilter).map(getDate);

this should return the results your looking for... or something close to this at least :)




回答2:


I should have read the docs more carefully. In addition to the pluck method in underscore, backbone collections also have a pluck method.

http://documentcloud.github.com/backbone/#Collection-pluck

So, my solution would be

//Assumme 'collection' is a backbone collection
collection.pluck('date');

I still need to figure out how to best combine with filtering - perhaps using ideas from @Derick's answer, but this answers the meat of my own question.




回答3:


I think this could work :

var days =
    _( this.collection.where({ type : "session" }))
    .chain()
    .pluck("attributes")
    .pluck("date")
    .value()

Slightly more elegant, but still close to unreadable, in my opinion.




回答4:


Have the same question, and figured out a better solution. I might have just taken advantage of new features in underscore, since it's 2013 now :)

var days = _.invoke(this.collection.where({type : 'session'}), 'get', 'date');



回答5:


This is functionally the same as Derick's answer using _.chain()

var days = _(this.collection.models)
            .chain()
            .filter(function(model){return model.attributes.type === 'session';})
            .pluck('date')
            .value();


来源:https://stackoverflow.com/questions/7587383/how-to-pluck-a-backbone-collections-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!