Array 分组
小于 1 分钟
Array
分组
模块
类型
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
示例
示例:
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]