通过使用underscore
或jQuery
我需要将maximum id
值增加一个包含在对象列表中的值。从对象列表中获取属性
JavaScript对象是Backbone.Collection
,它看起来像这样:
this.collection.models = [{attributes: {id: 1, ....}}, {}];
我写了下面的代码工作,但我想知道是否有改进它的任何变化。
谢谢。
getId: function() {
return _.max(
_.map(
_.pluck(this.collection.models, 'attributes'), function (attributes) {
return attributes.id;
})) + 1;
},
作者不需要一个对象,他需要一个增加1的id。 –