跳至主要內容

Array 分组

小于 1 分钟featurees-proposal

Array 分组open in new window

模块

类型

interface ObjectConstructor {
  groupBy<T>(
    items: Iterable<T>,
    callbackfn: (value: T, index: number) => string | number
  ): { [index: string]: Array<T> };
}
interface MapConstructor {
  groupBy<K, V>(
    items: Iterable<V>,
    callbackfn: (value: V, index: number) => K
  ): Map<K, Array<V>>;
}

入口点

core-js/proposals/array-grouping-v2
core-js(-pure)/full/map/group-by
core-js(-pure)/full/object/group-by

示例

示例open in new window:

Object.groupBy([1, 2, 3, 4, 5], (it) => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] }
const map = Map.groupBy([1, 2, 3, 4, 5], (it) => it % 2);
map.get(1); // => [1, 3, 5]
map.get(0); // => [2, 4]