Dev Helpers
← Back

Build a last arrival lookup

Build a last arrival lookup. 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/build-a-last-arrival-lookup/page.tsx
export const metadata = { title: "Build a last arrival lookup" };
export default function Page() {
  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 = Object.fromEntries(
    rows.toSorted((a, b) => a.end - b.end).map((r) => [r.line, r.end]),
  );
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Build a last arrival lookup"}</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>
  );
}