Dev Helpers
← Back

Identify rows with no reservations

Identify rows with no reservations. Parse newline-delimited records from a readable stream before applying the transformation.

Open this example in the interactive library

Source code

import { Readable } from "node:stream";
import { createInterface } from "node:readline";
const input = Readable.from([
  '{"row":"A","seat":1,"held":true}\n',
  '{"row":"A","seat":2,"held":false}\n',
  '{"row":"B","seat":1,"held":false}\n',
  '{"row":"B","seat":2,"held":false}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
  rows.push(JSON.parse(line));
}
const result = [...new Set(rows.map((r) => r.row))].filter((row) =>
  rows.filter((r) => r.row === row).every((r) => !r.held),
);
console.log(JSON.stringify(result, null, 2));

Sample output

[
  "B"
]