Open this example in the interactive library
Source code
// app/assign-the-first-tag-as-a-primary-category/page.tsx
export const metadata = { title: "Assign the first tag as a primary category" };
export default function Page() {
const rows = [
{ title: "Tide", tags: ["sea", "blue"] },
{ title: "Dune", tags: ["sand", "gold"] },
{ title: "Reef", tags: ["sea", "gold"] },
{ title: "Blank", tags: [] },
];
const result = Object.fromEntries(
rows.map((r) => [r.title, r.tags.at(0) ?? "untagged"]),
);
const entries = Object.entries(
typeof result === "object" && result !== null ? result : { value: result },
);
return (
<section>
<h2>{"Assign the first tag as a primary category"}</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>
);
}
