Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 1002 Bytes

groupBy.md

File metadata and controls

33 lines (24 loc) · 1002 Bytes
标题 标签
groupBy(元素分组) array,object(数组,对象)

根据给定的函数对数组的元素进行分组。

  • 使用 Array.prototype.map() 将数组的值映射到函数或属性名称。
  • 使用 Array.prototype.reduce() 创建一个对象,其中的键是从映射的结果中生成的。
const groupBy = (arr, handler) =>
  arr
    .map(typeof handler === 'function' ? handler : val => val[handler])
    .reduce((acc, val, i) => {
      acc[val] = (acc[val] || []).concat(arr[i]);
      return acc;
    }, {});

调用方式:

groupBy([6.1, 4.2, 6.3], Math.floor); // {4: [4.2], 6: [6.1, 6.3]}
groupBy(['one', 'two', 'three'], 'length'); // {3: ['one', 'two'], 5: ['three']}

应用场景

结果如下:

<iframe src="codes/javascript/html/groupBy.html"></iframe>