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

async.map which works on objects #374

Closed
tremby opened this issue Sep 9, 2013 · 6 comments
Closed

async.map which works on objects #374

tremby opened this issue Sep 9, 2013 · 6 comments

Comments

@tremby
Copy link

tremby commented Sep 9, 2013

I had a use case just now for async.map which iterates on the values of an object, and whose callback has a new object with the original keys pointing to the translated values of the same keys.

async.map as it exists doesn't allow this, and I couldn't see another method which does the same (please let me know if I missed it).

I think it'd be useful feature.

In the mean time, I faked it with underscore by doing async.map(_.values(obj), ... and then in the callback using _.object(_.keys(obj), transformed).

@Volox
Copy link

Volox commented Oct 8, 2013

+1

@glukki
Copy link

glukki commented Oct 31, 2013

+1 For now I use this wrapper

function asyncObjectMap( obj, func, cb ) {
    var i, arr = [], keys = Object.keys( obj );
    for ( i = 0; i < keys.length; i += 1 ) {
        arr[i] = obj[keys[i]];
    }
    async.map( arr, func, function( err, data ) {
        if ( err ) { return cb( err ); }

        var res = {};
        for ( i = 0; i < data.length; i += 1 ) {
            res[keys[i]] = data[i];
        }

        return cb( err, res );
    } );
}

// test
asyncObjectMap(
    { a: 1, b: 2 },
    function( item, cb ) {
        console.log( item );
        cb( null, item );
    },
    console.log
);

@caolan caolan closed this as completed Mar 28, 2014
@tremby
Copy link
Author

tremby commented Mar 28, 2014

It would be good to note the resolution of the ticket here -- reading through commit messages to find the answer is often not as easy as it sounds. Was this implemented or rejected?

@caolan
Copy link
Owner

caolan commented Mar 28, 2014

@tremby closed in favour of #168 - which I know isn't a map but seems like the right approach rather than varying the number of arguments to iterators

@tremby
Copy link
Author

tremby commented Mar 28, 2014

Great -- thanks.

@JumpLink
Copy link

Another Version of @glukki 's Wrapper:

  async.objectMap = function ( obj, func, cb ) {
    var i, arr = [], keys = Object.keys( obj );
    for ( i = 0; i < keys.length; i += 1 ) {
      var wrapper = {};
      wrapper[keys[i]] = obj[keys[i]];
      arr[i] = wrapper;
    }
    this.map( arr, func, function( err, data ) {
      if ( err ) { return cb( err ); }
      var res = {};
      for ( i = 0; i < data.length; i += 1 ) {
          res[keys[i]] = data[i];
      }
      return cb( err, res );
    });
  }

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

5 participants