Open this example in the interactive library
Source code
import { Readable } from "node:stream";
import { createInterface } from "node:readline";
const input = Readable.from([
'{"task":"design","needs":[]}\n',
'{"task":"build","needs":["design"]}\n',
'{"task":"test","needs":["build"]}\n',
'{"task":"ship","needs":["build","test"]}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
rows.push(JSON.parse(line));
}
const result = rows.flatMap((r) =>
r.needs.map((parent) => ({ from: parent, to: r.task })),
);
console.log(JSON.stringify(result, null, 2));
Sample output
[
{
"from": "design",
"to": "build"
},
{
"from": "build",
"to": "test"
},
{
"from": "build",
"to": "ship"
},
{
"from": "test",
"to": "ship"
}
]