Dev Helpers
← Back

Build the second-choice transfer list

Build the second-choice transfer list. 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 = [
    { name: "Ada", picks: ["A", "C", "B"] },
    { name: "Kim", picks: ["C", "A", "B"] },
    { name: "Leo", picks: ["A", "B", "C"] },
  ];
  const result = rows
    .filter((r) => r.picks[0] === "A")
    .map((r) => ({ voter: r.name, transfer: r.picks[1] }));
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Build the second-choice transfer list"}</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>
  );
}