Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
How to group arrays alphabetically with JavaScript?

I have an array that has several other arrays inside it. All data is already sorted alphabetically.

What I would like to know is how can I group alphabetically?

Just like in the example image below:

enter image description here

Here's my project I put into codesandbox

import "./styles.css";

import { data } from "./data";

export default function App() {
  console.log("data: ", data);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      {data.map((item, index) => (
        <div key={index}>
          {item.map((item2, index2) => (
            <div key={index2}>
              <span>{item2.title}</span>
            </div>
          ))}
        </div>
      ))}
    </div>
  );
}

Thank you in advance for any help!!!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

TL;DR. See the Code Sandbox I cloned and edited from yours.

In summary, I did something like below:

import "./styles.css";

import { data } from "./data";

export default function App() {
  // Flatten `data` array
  const merged = [];
  data.map((arr) => arr.map((item) => merged.push(item)));

  // Reduce `merged` array by initial character of `title`
  const mapped = merged.reduce((acc, item) => {
    const letter = item.title[0].toLowerCase();
    if (!acc[letter]) {
      acc[letter] = [];
    }
    acc[letter].push(item);
    return acc;
  }, {});

  const letters = Object.keys(mapped);

  return (
    <div className="App">
      <h1>Grouped by the first initial</h1>
      {letters.map((letter, i) => (
        <div key={i}>
          <h2>{letter}</h2>
          {mapped[letter].map((item, j) => (
            <div key={j}>{item.title}</div>
          ))}
        </div>
      ))}
    </div>
  );
}
  1. Flatten data array into merged variable.
  2. Reduce the merged into a dictionary-like object (by each first initial) into mapped variable.
  3. Then render by mapping the merged array.

See it in action in the Code Sandbox.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda