countBy.js 510 B

123456789101112131415161718192021
  1. var groupBy = require('./groupBy')
  2. var objectEach = require('./objectEach')
  3. /**
  4. * 集合分组统计,返回各组中对象的数量统计
  5. *
  6. * @param {Array} obj 对象
  7. * @param {Function} iterate 回调/对象属性
  8. * @param {Object} context 上下文
  9. * @return {Object}
  10. */
  11. function countBy (obj, iterate, context) {
  12. var result = groupBy(obj, iterate, context || this)
  13. objectEach(result, function (item, key) {
  14. result[key] = item.length
  15. })
  16. return result
  17. }
  18. module.exports = countBy