Skip to content

groupBy

js
_.groupBy(collection, [iteratee=_.identity])

创建一个由运行 collectioniteratee 的每个元素的结果生成的键组成的对象。分组值的顺序由它们在 collection 中出现的顺序决定。每个键的对应值是负责生成键的元素数组。迭代器使用一个参数调用:(value)。

¥Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).

新增于

¥Since

0.1.0

参数

¥Arguments

  1. collection (数组|对象):要迭代的集合。

    ¥collection (Array|Object): The collection to iterate over.

  2. [iteratee=_.identity] (函数):用于转换键的迭代器。

    ¥[iteratee=_.identity] (Function): The iteratee to transform keys.

返回

¥Returns

(对象):返回组合的聚合对象。

¥(Object): Returns the composed aggregate object.

示例

¥Example

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

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

Lodash v4.17 中文网 - 粤ICP备13048890号