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 = Object.fromEntries(
[...new Set(rows.map((r) => r.hour))].map((h) => [
h,
Object.fromEntries(
rows.filter((r) => r.hour === h).map((r) => [r.site, r.value]),
),
]),
);
console.log(JSON.stringify(result, null, 2));
Sample output
{
"8": {
"N": 12,
"S": 17
},
"9": {
"N": 15,
"S": 14
}
}