Dev Helpers
← Back

Grouped initials index

Import and render <GroupedInitialsIndex /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/GroupedInitialsIndex.tsx

export default function GroupedInitialsIndex({
  items = ["Oak", "Pine", "Maple"],
}: { items?: string[] } = {}) {
  const groups = Map.groupBy(items, (v) => v.slice(0, 1).toUpperCase());
  return (
    <>
      {[...groups].map(([letter, values]) => (
        <section key={letter}>
          <h2>{letter}</h2>
          <ul>
            {values.map((v, i) => (
              <li key={i}>{v}</li>
            ))}
          </ul>
        </section>
      ))}
    </>
  );
}