Dev Helpers
← Back

Calculate occupancy ratios per row

Calculate occupancy ratios per 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) => {
    const seats = rows.filter((r) => r.row === row);
    return [row, seats.filter((r) => r.held).length / seats.length];
  }),
);
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "A": 0.5,
  "B": 0
}