Dev Helpers
← Back

Expand service windows into hourly slots

Expand service windows into hourly slots. Inspect the transformed data in the console.

Open this example in the interactive library

Source code

const rows = [
  { 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((r) =>
  Array.from({ length: r.end - r.start }, (_, i) => ({
    line: r.line,
    hour: r.start + i,
  })),
);
console.log(JSON.stringify(result, null, 2));

Sample output

[
  {
    "line": "Red",
    "hour": 8
  },
  {
    "line": "Red",
    "hour": 9
  },
  {
    "line": "Blue",
    "hour": 9
  },
  {
    "line": "Blue",
    "hour": 10
  },
  {
    "line": "Red",
    "hour": 12
  },
  {
    "line": "Red",
    "hour": 13
  },
  {
    "line": "Red",
    "hour": 14
  },
  {
    "line": "Gold",
    "hour": 15
  }
]