Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

countBy.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var baseAssignValue = require('./_baseAssignValue'),
  2. createAggregator = require('./_createAggregator');
  3. /** Used for built-in method references. */
  4. var objectProto = Object.prototype;
  5. /** Used to check objects for own properties. */
  6. var hasOwnProperty = objectProto.hasOwnProperty;
  7. /**
  8. * Creates an object composed of keys generated from the results of running
  9. * each element of `collection` thru `iteratee`. The corresponding value of
  10. * each key is the number of times the key was returned by `iteratee`. The
  11. * iteratee is invoked with one argument: (value).
  12. *
  13. * @static
  14. * @memberOf _
  15. * @since 0.5.0
  16. * @category Collection
  17. * @param {Array|Object} collection The collection to iterate over.
  18. * @param {Function} [iteratee=_.identity] The iteratee to transform keys.
  19. * @returns {Object} Returns the composed aggregate object.
  20. * @example
  21. *
  22. * _.countBy([6.1, 4.2, 6.3], Math.floor);
  23. * // => { '4': 1, '6': 2 }
  24. *
  25. * // The `_.property` iteratee shorthand.
  26. * _.countBy(['one', 'two', 'three'], 'length');
  27. * // => { '3': 2, '5': 1 }
  28. */
  29. var countBy = createAggregator(function(result, value, key) {
  30. if (hasOwnProperty.call(result, key)) {
  31. ++result[key];
  32. } else {
  33. baseAssignValue(result, key, 1);
  34. }
  35. });
  36. module.exports = countBy;