Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async.js each get index in iterator

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 }) 
like image 254
Kevin Avatar asked Jul 07 '13 22:07

Kevin


1 Answers

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.

like image 190
xuanji Avatar answered Sep 23 '22 06:09

xuanji