Namespace: groupBy

async.groupBy

Source:

Examples

// array
var order = [];
var array = [4.2, 6.4, 6.1];
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupBy(array, iterator, function(err, res) {
  console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  console.log(order); // [4.2, 6.1, 6.4]
});
// array with index
var order = [];
var array = [4.2, 6.4, 6.1];
var iterator = function(num, index, done) {
  setTimeout(function() {
    order.push([num, index]);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupBy(array, iterator, function(err, res) {
  console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  console.log(order); // [[4.2, 0], [6.1, 2], [6.4, 1]]
});
// object
var order = [];
var object = { a: 4.2, b: 6.4, c: 6.1 };
var iterator = function(num, done) {
  setTimeout(function() {
    order.push(num);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupBy(object, iterator, function(err, res) {
  console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  console.log(order); // [4.2, 6.1, 6.4]
});
// object with key
var order = [];
var object = { a: 4.2, b: 6.4, c: 6.1 };
var iterator = function(num, key, done) {
  setTimeout(function() {
    order.push([num, key]);
    done(null, Math.floor(num));
  }, num * 10);
};
async.groupBy(object, iterator, function(err, res) {
  console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  console.log(order); // [[4.2, 'a'], [6.1, 'c'], [6.4, 'b']]
});