gpt4 book ai didi

javascript - 遍历集合中的数组

转载 作者:行者123 更新时间:2023-11-30 06:28:18 24 4
gpt4 key购买 nike

所以,我对 Backbone 还是很陌生。我的问题是:我有一个这样制作的集合:

...
isCollection: true,
parse: function(response) {
if (response.status == 'ok') {
this.page = response.result.page;
this.count = response.result.count;
this.total = response.result.total;
this.sort = response.result.sort;
this.ascending = response.result.ascending;
this.myReports = response.result.results;
return this;
} else {

}

其中 this.myReports 是一个对象数组。我的问题是如何遍历数组 (myReports)?我必须将其转换为集合吗?如果我尝试使用 .each,我会收到关于 .each 不受支持的错误。

感谢您的帮助!

最佳答案

当您在 Backbone 集合上调用 myCollection.each 时,您实际上是在调用 Underscore 的 each方法,但它以 Backbone.Collection 方法的形式更好地呈现给您。

您可以只在数组上使用该方法:

_.each(this.myReports, function (report) {
// do whatever with report
})

是否应该将其转换为 Backbone 集合是另一个问题。如果您想在其中存储模型,或使用任何 Backbone.Collection 功能,那么当然可以使用上面的 each 方法转换它。

关于javascript - 遍历集合中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19569394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com