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

86
Vistas
JS iterate array items and group them in new array by property

I have an array of objects

    listOfItems: any[] = [
      {text: 'Hola', group: 'espanian'},
      {text: 'Hello', group: 'english'},
      {text: 'How are you', group: 'english'},
      {text: 'Tere', group: 'estonian'},
    ]

I would like to iterate over an array and create another array with categorized objects

    groups: any[] = [
    [
      { name: 'english', 
        items: [
           {text: 'Hello', group: 'english'},
           {text: 'How are you', group: 'english'},
        ]
      },
      { name: 'espanian', 
        items: [
           {text: 'Hola', group: 'espanian'}
        ]
      },
      { name: 'estonian', 
        items: [
           {text: 'Tere', group: 'estonian'}
        ]
      },
    ]

What is the most dynamic way to do it? The number of groups could be over 30, so I would like to avoid doing if or equals checks inside the loop

I mean dynamic - is basically without any explicit checks like item.group = 'english'

I have tried to achieve something similar with

      let result: any = [];
      this.listOfItems.forEach(p => {
        var key = p.group;
        result[key] = result[key] || [];
        result[key].push(p);
      })

however the result is not exactly what I am looking for

[
   {'english': [
             {text: 'Hello', group: 'english'},
             {text: 'How are you', group: 'english'},
            ]
   }, 
   {'espanian': [
             {text: 'Hola', group: 'espanian'},
            ]
   },
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Array.reduce implementation.

const listOfItems = [
  { text: 'Hola', group: 'espanian' },
  { text: 'Hello', group: 'english' },
  { text: 'How are you', group: 'english' },
  { text: 'Tere', group: 'estonian' },
];

const output = listOfItems.reduce((acc, curr) => {
  const matchItem = acc.find(item => item.name === curr.group);
  if (matchItem) {
    matchItem.items.push(curr);
  } else {
    acc.push({
      name: curr.group,
      items: [curr]
    })
  }
  return acc;
}, []);
console.log(output);

For those who are concerned about double loop, you can achieve the above one with a single loop using.

const listOfItems = [
  { text: 'Hola', group: 'espanian' },
  { text: 'Hello', group: 'english' },
  { text: 'How are you', group: 'english' },
  { text: 'Tere', group: 'estonian' },
];

const output = listOfItems.reduce((acc, curr) => {
  acc[curr.group] ? acc[curr.group].items.push(curr) : acc[curr.group] = {
    name: curr.group,
    items: [curr]
  };
  return acc;
}, {});
console.log(Object.values(output));

about 4 years ago · Juan Pablo Isaza Denunciar

0

  1. Why any[]?
  2. Maps are really great for these types of data manipulations. Check it out:
interface Item {
    text: string;
    group: string;
}

interface Categorized {
    name: string;
    items: Item[];
}

const itemList: Item[] = [
    { text: 'Hola', group: 'espanian' },
    { text: 'Hello', group: 'english' },
    { text: 'How are you', group: 'english' },
    { text: 'Tere', group: 'estonian' },
];

const categorize = (list: Item[]): Categorized[] => {
    const map: Record<string, Categorized> = {};

    list.forEach((item) => {
        if (!map[item.group]) {
            map[item.group] = { name: item.group, items: [item] };
            return;
        }

        map[item.group].items.push(item);
    });

    return Object.values(map);
};

console.log(categorize(itemList));

We're only using one loop.

Compiled:

var itemList = [
    { text: 'Hola', group: 'espanian' },
    { text: 'Hello', group: 'english' },
    { text: 'How are you', group: 'english' },
    { text: 'Tere', group: 'estonian' },
];
var categorize = function (list) {
    var map = {};
    list.forEach(function (item) {
        if (!map[item.group]) {
            map[item.group] = { name: item.group, items: [item] };
            return;
        }
        map[item.group].items.push(item);
    });
    return Object.values(map);
};
console.log(categorize(itemList));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try with the below code

const modifiedList = listOfItems.reduce((acc, item) => {
 if(!acc[item.group]) acc[item.group]=[item];
 else acc[item.group].push(item);
 return acc;
}, {});
let newList = [];
for(let item in modifiedList) {
 newList.push({
   name: item,
   items: modifiedList[item]
 })
}
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