countBy.js 376 B

123456789101112
  1. var _group = require('./_group.js');
  2. var _has = require('./_has.js');
  3. // Counts instances of an object that group by a certain criterion. Pass
  4. // either a string attribute to count by, or a function that returns the
  5. // criterion.
  6. var countBy = _group(function(result, value, key) {
  7. if (_has(result, key)) result[key]++; else result[key] = 1;
  8. });
  9. module.exports = countBy;