Dev Helpers
← Back

Pack time series as ordered coordinate pairs

Pack time series as ordered coordinate pairs. Build the result in an App Router server page and send the rendered entries to the client.

Open this example in the interactive library

Source code

// app/pack-time-series-as-ordered-coordinate-pairs/page.tsx
export const metadata = {
  title: "Pack time series as ordered coordinate pairs",
};
export default function Page() {
  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.site))].map((site) => [
      site,
      rows.filter((r) => r.site === site).map((r) => [r.hour, r.value]),
    ]),
  );
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Pack time series as ordered coordinate pairs"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}