从javascript for循环到下划线(From javascript for loop to underscore each)

编程入门 行业动态 更新时间:2024-10-15 22:30:48
从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); });

更多推荐

本文发布于:2023-08-06 12:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1449386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下划线   javascript   underscore   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!