Open this example in the interactive library
Live preview
Source code
// CollectionView.tsx
export default function CollectionView() {
const rows = [
{ item: "tea", units: 2, cents: 350 },
{ item: "mug", units: 1, cents: 900 },
{ item: "tea", units: 3, cents: 325 },
{ item: "tin", units: 2, cents: 500 },
];
const result = Object.fromEntries(
[...new Set(rows.map((r) => r.item))].map((item) => {
const prices = rows.filter((r) => r.item === item).map((r) => r.cents);
return [item, { min: Math.min(...prices), max: Math.max(...prices) }];
}),
);
const entries = Object.entries(
typeof result === "object" && result !== null ? result : { value: result },
);
return (
<section>
<h2>{"Build price bands for repeated items"}</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>
);
}
