1
0

group.js 559 B

1234567891011121314151617181920212223
  1. // Inspired by Underscore's groupBy:
  2. // http://documentcloud.github.com/underscore/#groupBy
  3. 'use strict';
  4. var callable = require('../../object/valid-callable')
  5. , value = require('../../object/valid-value')
  6. , forEach = Array.prototype.forEach, apply = Function.prototype.apply;
  7. module.exports = function (cb/*, thisArg*/) {
  8. var r;
  9. (value(this) && callable(cb));
  10. r = {};
  11. forEach.call(this, function (v) {
  12. var key = apply.call(cb, this, arguments);
  13. if (!r.hasOwnProperty(key)) r[key] = [];
  14. r[key].push(v);
  15. }, arguments[1]);
  16. return r;
  17. };