Dev Helpers
← Back

Find rows with an adjacent free pair

Find rows with an adjacent free pair. Inspect the transformed data in the console.

Open this example in the interactive library

Source code

const rows = [
  { 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 = rows
  .filter(
    (r) =>
      !r.held &&
      rows.some((x) => x.row === r.row && !x.held && x.seat === r.seat + 1),
  )
  .map((r) => ({ row: r.row, pair: [r.seat, r.seat + 1] }));
console.log(JSON.stringify(result, null, 2));

Sample output

[
  {
    "row": "B",
    "pair": [
      1,
      2
    ]
  }
]