@tc39-proposal/group-by
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Install

Install @tc39-proposal/group-by by pnpm

pnpm add @tc39-proposal/group-by

Usage

Group by Object

import { groupBy } from "@tc39-proposal/group-by";

const array = [1, 2, 3, 4, 5];

// `groupBy(array, fn)` groups items by arbitrary key.
// In this case, we're grouping by even/odd keys
const result1 = groupBy(array, (num, index) => {
  return num % 2 === 0 ? "even" : "odd";
});
console.info(result1);
// =>  { odd: [1, 3, 5], even: [2, 4] }

you can also use

import { groupByObject } from "@tc39-proposal/group-by";

Group by Map

import { groupBy } from "@tc39-proposal/group-by";

// `groupBy(array, fn, true)` returns items in a Map, and is useful for grouping
// using an object key.
const odd = { odd: true };
const even = { even: true };
const result2 = groupBy(
  array,
  (num, index) => {
    return num % 2 === 0 ? even : odd;
  },
  true
);
console.info(result2);
// => Map(2) { { odd: true } => [ 1, 3, 5 ], { even: true } => [ 2, 4 ] }

you can also use

import { groupByMap } from "@tc39-proposal/group-by";

Others

Welcome to contribute and make @tc39-proposal/group-by better!

Package Sidebar

Install

npm i @tc39-proposal/group-by

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

3.14 kB

Total Files

5

Last publish

Collaborators

  • simumn