async.js各获得迭代指数 [英] async.js each get index in iterator

查看:158
本文介绍了async.js各获得迭代指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 caolan的async.js 库,具体的方法。每个

你如何获得访问索引中的迭代器?

  async.each(元,函数(元素,回调){
  //为进制每个元素做的东西在这里
  //我如何才能访问索引?
},功能(错误){
  //此处最终的回调
})


解决方案

您可以使用 async.forEachOf - 它所谓的迭代器回调的指数作为其第二个参数。

 > async.forEachOf(['一','B','C'],函数(){的console.log(参数)});
{'0':'一','1':0,'2':[功能]}
{'0':'B','1':1,2':[功能]}
{'0':'C','1':2,'2':[功能]}

I'm using caolan's async.js library, specifically the .each method.

How do you get access to the index in the iterator?

async.each(ary, function(element, callback){
  //do stuff here for each element in ary
  //how do I get access to the index?
}, function(err) {
  //final callback here
})

解决方案

You can use async.forEachOf - it calls its iterator callback with the index as its second argument.

> async.forEachOf(['a', 'b', 'c'], function () {console.log(arguments)});
{ '0': 'a', '1': 0, '2': [Function] }
{ '0': 'b', '1': 1, '2': [Function] }
{ '0': 'c', '1': 2, '2': [Function] }

这篇关于async.js各获得迭代指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆