Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you add the index of array in async.forEach and async.forEachSeries? #213

Closed
llevkin opened this issue Jan 6, 2013 · 6 comments
Closed

Comments

@llevkin
Copy link

llevkin commented Jan 6, 2013

Now async.forEach and async.forEachSeries looks like this:

async.forEachSeries(array, iterator, callback);

iterator has two arguments:

function iterator(item, callback) {}

If I need find a key (index) of item in array by value, how i can get it?
For async.forEachSeries I can declare my own varable count and increment it in the end of iterator-function, I think it not quite right, but it's possible.. For async.forEach function it's not possible to detect a key (index) of array value...( For example, how I can remove some items from array with async, if i don't know a key of value?

Maybe it makes sense to add in iterator function a third argument key (index)? Somthing like this:

function iterator(item, callback, key) {} - it's not so beauty, but useful.

Great Regards! sorry for my English.

@llevkin
Copy link
Author

llevkin commented Jan 6, 2013

sorry)))) forgot about indexOf...

@brianmaissy
Copy link
Contributor

This should be closed as a duplicate of #168

@caolan
Copy link
Owner

caolan commented Feb 6, 2013

@llevkin see my comments on function arity in #168 - further discussion on this should happen there. Thanks @brianmaissy, I'll close this one.

@caolan caolan closed this as completed Feb 6, 2013
@Markkos89
Copy link

there is an issue where you have 2 identical rows and the indexOf will throw a wrong index....

would be so much usefull to have and index iterator in the function..

@aearly
Copy link
Collaborator

aearly commented Oct 5, 2017

async.eachOf and async.eachOfSeries will include the array index.

@knoxcard
Copy link

knoxcard commented May 29, 2018

I came across this today, which I think is absolutely brilliant...in case you are needing the ID.
If there is something more simple than this, please let me know.

async.each(Object.keys(arr), function(index, cb) {
  console.log('index: ' + index)
  var item = arr[index]
  console.log(item)
  cb()
}, (err, ret) {
  console.log('done')
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants