从javascript for循环到下划线(From javascript for loop to underscore each)
我有一个Angular应用程序,我正在循环一个集合。 我正在使用经典的for循环,但是喜欢使用更新的东西。 underscore.js已经包含在项目中了,我看了一下使用_.each
这是我当前(工作)的js代码:
Batches.create($scope.newPanel).then(function(panel) { console.log('panel created: ' + panel.id); for(i = 0; i < $scope.selector.tabledata.length; i++){ createPanelDetails(panel.id, $scope.selector.tabledata[i]); }我想使用_.each,但是我如何传递panel.id变量和for循环一样?
_.each($scope.selector.tabledata, createPanelDetails(panel.id ??, iterator???);或者我应该使用下划线的其他方法来实现这一目标吗?
I have an Angular app where I am looping through a collection. I am using a classic for loop, but for like to use something more up to date. underscore.js is already included in the project, and I took a look at using _.each
This is my current (working) js code:
Batches.create($scope.newPanel).then(function(panel) { console.log('panel created: ' + panel.id); for(i = 0; i < $scope.selector.tabledata.length; i++){ createPanelDetails(panel.id, $scope.selector.tabledata[i]); }I would like to use the _.each, but how do I pass in the panel.id variable as I do with the for loop?
_.each($scope.selector.tabledata, createPanelDetails(panel.id ??, iterator???);Or should I be using some other method from underscore to achieve this?
最满意答案
使用内联函数:
_.each($scope.selector.tabledata, function(data) { createPanelDetails(panel.id, data); });Use an inline function:
_.each($scope.selector.tabledata, function(data) { createPanelDetails(panel.id, data); });更多推荐
发布评论