Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Chain Backbone.js收集方法_Backbone.js_Underscore.js - Fatal编程技术网

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的方法,这意味着,如果您查看方法的源代码,您将看到它使用下划线.js
filter
方法,因此,这意味着您可以实现您想要的:

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);