Dev Helpers
← Back

Group free seat numbers by row

Group free seat numbers by row. Start with typed records and inspect the inferred result.

Open this example in the interactive library

Source code

type Row = { row: string; seat: number; held: boolean };
const rows: Row[] = [
  { row: "A", seat: 1, held: true },
  { row: "A", seat: 2, held: false },
  { row: "B", seat: 1, held: false },
  { row: "B", seat: 2, held: false },
];
const result = Object.fromEntries(
  [...new Set(rows.map((r) => r.row))].map((row) => [
    row,
    rows.filter((r) => r.row === row && !r.held).map((r) => r.seat),
  ]),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "A": [
    2
  ],
  "B": [
    1,
    2
  ]
}