BACK
PracticeReal-life: groupBy() tickets by status
PRACTICE · 42 · 練

Real-life: groupBy() tickets by status

Group tickets by status and output the count in each group.

  1. Solve the task in the editor — no long theory.
  2. Run the check and compare your output with the expected result.
  3. Stuck? Open the solution and carry the approach into your own code.
Solution spoiler · click to reveal
const { from, groupBy, mergeMap, toArray, map } = Rx;

const tickets = [
  { status: 'open' },
  { status: 'closed' },
  { status: 'open' },
];

const result$ = from(tickets).pipe(
  groupBy(ticket => ticket.status),
  mergeMap(group$ => group$.pipe(
    toArray(),
    map(items => group$.key + ':' + items.length)
  ))
);

result$.subscribe(value => console.log(value));
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...