Dev Helpers
← Back

Create labels for threshold crossings

Create labels for threshold crossings. Start with typed records and inspect the inferred result.

Open this example in the interactive library

Source code

type Row = { site: string; hour: number; value: number };
const rows: Row[] = [
  { site: "N", hour: 8, value: 12 },
  { site: "S", hour: 8, value: 17 },
  { site: "N", hour: 9, value: 15 },
  { site: "S", hour: 9, value: 14 },
];
const result = [...new Set(rows.map((r) => r.site))].flatMap((site) => {
  const a = rows.filter((r) => r.site === site);
  return a
    .slice(1)
    .filter((r, i) => a[i].value < 15 && r.value >= 15)
    .map((r) => ({ site, hour: r.hour }));
});
console.log(JSON.stringify(result, null, 2));

Sample output

[
  {
    "site": "N",
    "hour": 9
  }
]