主题
countBy
js
_.countBy(collection, [iteratee=_.identity])
创建一个由运行 collection
至 iteratee
的每个元素的结果生成的键组成的对象。每个键的对应值是 iteratee
返回该键的次数。迭代器使用一个参数调用:(value)。
¥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
collection
(数组|对象):要迭代的集合。¥
collection
(Array|Object): The collection to iterate over.[iteratee=_.identity]
(函数):用于转换键的迭代器。¥
[iteratee=_.identity]
(Function): The iteratee to transform keys.
返回
¥Returns
(对象):返回组合的聚合对象。
¥(Object): Returns the composed aggregate object.
示例
¥Example
js
_.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 }