Dev Helpers
← Back

List bins carrying the same part

List bins carrying the same part. Render the transformed entries in a reusable React component.

Open this example in the interactive library

Live preview

Source code

// CollectionView.tsx
export default function CollectionView() {
  const rows = [
    { sku: "pen", bin: "A", qty: 4 },
    { sku: "pad", bin: "B", qty: 0 },
    { sku: "pen", bin: "B", qty: 3 },
    { sku: "clip", bin: "A", qty: 9 },
  ];
  const result = Object.fromEntries(
    [...new Set(rows.map((r) => r.sku))].map((sku) => [
      sku,
      rows.filter((r) => r.sku === sku).map((r) => r.bin),
    ]),
  );
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"List bins carrying the same part"}</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>
  );
}