Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!