async.js each get index in iterator

后端 未结 6 1745
一整个雨季
一整个雨季 2021-02-02 07:04

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(elemen         


        
6条回答
  •  不要未来只要你来
    2021-02-02 07:57

    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] }
    

    see the docs for more info.

提交回复
热议问题