Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda