groupBy source npm

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

创建一个对象组成,key 是经 iteratee 处理的结果, value 是产生 key 的元素数组。 iteratee 会传入1个参数:(value)。

参数

  1. collection (Array|Object)

    需要遍历的集合

  2. [iteratee=_.identity] (Function|Object|string)

    这个函数会处理每一个元素

返回值 (Object)

返回一个组成汇总的对象

示例

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

// 使用了 `_.property` 的回调结果
_.groupBy(['one', 'two', 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }