Dev Helpers
← Back

Identify the warmest station each hour

Identify the warmest station each hour. Inspect the transformed data in the console.

Open this example in the interactive library

Source code

const rows = [
  { 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.hour))].map(
  (h) =>
    rows.filter((r) => r.hour === h).toSorted((a, b) => b.value - a.value)[0],
);
console.log(JSON.stringify(result, null, 2));

Sample output

[
  {
    "site": "S",
    "hour": 8,
    "value": 17
  },
  {
    "site": "N",
    "hour": 9,
    "value": 15
  }
]