Dev Helpers
← Back

Collect ballots preferring A over C

Collect ballots preferring A over C. 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([
  '{"name":"Ada","picks":["A","C","B"]}\n',
  '{"name":"Kim","picks":["C","A","B"]}\n',
  '{"name":"Leo","picks":["A","B","C"]}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
  rows.push(JSON.parse(line));
}
const result = rows
  .filter((r) => r.picks.indexOf("A") < r.picks.indexOf("C"))
  .map((r) => r.name);
console.log(JSON.stringify(result, null, 2));

Sample output

[
  "Ada",
  "Leo"
]