4.17.11
3.10.1
2.4.2
1.3.1

_.countBy(collection, [iteratee=_.identity])

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

argument
collection
[iteratee=_.identity]
type
(Array|Object)
Function
description
The collection to iterate over.
The iteratee to transform keys.

Returns

(array)

Example

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

// The `_.property` iteratee shorthand.
_.countBy(
  ["one", "two", "three"],
  "length"
);
// => { '3': 2, '5': 1 }
_.countBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': 1, '6': 2 }

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