Skip to main content

countBy

_.countBy(iteratee, collection) # Ⓢ [Ⓣ][1] Ⓝ

Creates an object composed of keys generated from the results of running



each element of collection thru iteratee. The corresponding value of



each key is the number of times the key was returned by iteratee. The



iteratee is invoked with one argument: (value).

Since​

0.5.0

Arguments​

  1. iteratee (Function): The iteratee to transform keys.
  2. collection (Array|Object): The collection to iterate over.

Returns​

(Object): Returns the composed aggregate object.

Example​

_.countBy(Math.floor, [6.1, 4.2, 6.3]);
// => { '4': 1, '6': 2 }

// The `_.property` iteratee shorthand.
_.countBy('length', ['one', 'two', 'three']);
// => { '3': 2, '5': 1 }