Open this example in the interactive library
Source code
type Row = { line: string; start: number; end: number };
const rows: Row[] = [
{ line: "Red", start: 8, end: 10 },
{ line: "Blue", start: 9, end: 11 },
{ line: "Red", start: 12, end: 15 },
{ line: "Gold", start: 15, end: 16 },
];
const result = rows.flatMap((a) =>
rows
.filter((b) => a.end === b.start)
.map((b) => ({ from: a.line, to: b.line, hour: a.end })),
);
console.log(JSON.stringify(result, null, 2));
Sample output
[
{
"from": "Red",
"to": "Gold",
"hour": 15
}
]