question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

async.map which works on objects

See original GitHub issue

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).

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
JumpLinkcommented, Oct 15, 2014

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 );
    });
  }
0reactions
trembycommented, Mar 28, 2014

Great – thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best way to call an asynchronous function within map?
To make filter, some and every work you can first use an async map (that in turn uses Promise.all) and then go through...
Read more >
How to use async functions with Array.map in Javascript
The map is the easiest and most common collection function. It runs each element through an iteratee function and returns an array with...
Read more >
How to use Async and Await with Array.prototype.map()
You want to execute an async function inside a map() call, to perform an operation on every element of the array, and get...
Read more >
Async map with limited parallelism in Node.js - Alex Ewerlöf
Async map with limited parallelism in Node.js ... A Array.map() is a very useful function but, unfortunately, it only works with synchronous functions....
Read more >
Use async / await with Javascript's .map() and other high- ...
The .map() algorithm applies an async callback to each element of an array, creating promises as it does. However, the returned result by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found