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 =
["B", "A"].flatMap((row) =>
rows.filter((r) => r.row === row && !r.held),
)[0] ?? null;
console.log(JSON.stringify(result, null, 2));
Sample output
{
"row": "B",
"seat": 1,
"held": false
}