async.js each get index in iterator

后端 未结 6 1691
一整个雨季
一整个雨季 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:38

    1. indexOf solution is slow and should not be used for large arrays.
    2. eachSeries solution by Merc does't make what you want.
    3. Effective workaround is just to build another array with indexes:
    someArrayWithIndexes = someArray.map(function(e, i) {return {obj: e, index: i}});
    async.each(someArrayWithIndexes , function(item, done){
        console.log("Index:", item.index);
        console.log("Object:", item.obj);
    });
    

提交回复
热议问题