Chain Backbone.js收集方法
如何在中链接收集方法 我试过这样的方法,但是错了:Chain Backbone.js收集方法,backbone.js,underscore.js,Backbone.js,Underscore.js,如何在中链接收集方法 我试过这样的方法,但是错了: Object[object Object],[object Object],[object Object] has no method 'groupBy' 您无法以这种方式访问Backbone.Collection方法(希望我没有错),但您可能知道,大多数主干方法都是基于下划线.js的方法,这意味着,如果您查看方法的源代码,您将看到它使用下划线.jsfilter方法,因此,这意味着您可以实现您想要的: var filteredResults
Object[object Object],[object Object],[object Object] has no method 'groupBy'
您无法以这种方式访问
Backbone.Collection
方法(希望我没有错),但您可能知道,大多数主干方法都是基于下划线.js的方法,这意味着,如果您查看方法的源代码,您将看到它使用下划线.jsfilter
方法,因此,这意味着您可以实现您想要的:
var filteredResults = this.collection.chain()
.filter(function(model) { return model.get('county') == yourCounty; })
.groupBy(function(model) { return model.get('city') })
.each(function(model) { console.log(model); })
.value();
.value()
在这里对您没有任何用处,您正在中制作“东西”。每个模型的每个方法,但如果您想返回一个过滤城市数组,您可以使用。map
和中的filteredresult
将是您的结果
var filteredResults = this.collection.chain()
.filter(function(model) { return model.get('county') == yourCounty; })
.map(function(model) { return model.get('city'); })
.value();
console.log(filteredResults);
var filteredResults = this.collection.chain()
.filter(function(model) { return model.get('county') == yourCounty; })
.map(function(model) { return model.get('city'); })
.value();
console.log(filteredResults);