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

243
Vistas
In ReactJS is there a way to iterate through 2 arrays and combine the 2 arrays to be grouped by the data in it?

The title might be a little confusing, but basically I'm working on a project and it requires me to have 2 array of object lists. I have a group array and a values array, I've been looking for hours for something that might work but I haven't found anything yet!

I'm needing a direction to take these 2 lists and combine them to show some stuff for the UI, I'm using ReactJS for my frontend (if that helps) and ES6. I'll try to describe the best I can for the data I need and somehow iterate it through a list that categorize from the main group and the data that matches will be sent to an array under that group and keep going until the data belongs to another group.

groupedObjects = {
  groupI: {
    id: 1,
    name: 'GroupI',
    array: [
       {category: 'groupI', name: 'someValue1'},
       {category: 'groupI', name: 'someValue2'},
       {category: 'groupI', name: 'someValue3'},
       {category: 'groupI', name: 'someValue4'}
]},
  groupII: {
    id: 2,
    name: 'GroupII',
    array: [
       {category: 'groupII', name: 'someValue1'},
       {category: 'groupII', name: 'someValue2'},
       {category: 'groupII', name: 'someValue3'},
       {category: 'groupII', name: 'someValue4'}
]},
  groupIII: {
    id: 3,
    name: 'GroupIII',
    array: [
       {category: 'groupIII', name: 'someValue1'},
       {category: 'groupIII', name: 'someValue2'},
       {category: 'groupIII', name: 'someValue3'},
       {category: 'groupIII', name: 'someValue4'}
]}};

And then somehow iterate that on a list of tables to display to the UI showing the groupI, groupII, groupIII as separate tables in each table it has the data from the array in each object.

Edit: Here's the data I needed to combine

const groups = [
   {
      id: 1,
      name: 'GroupI'
   },
   {
      id: 2,
      name: 'GroupII'
   },
   {
      id: 3,
      name: 'GroupIII'
   },
];

const data = [
   {
      category: 'groupI',
      name: 'someValue1'
   },
   {
      category: 'groupI',
      name: 'someValue2'
   },
   {
      category: 'groupI',
      name: 'someValue3'
   },
   {
      category: 'groupI',
      name: 'someValue4'
   },
   {
      category: 'groupII',
      name: 'someValue1'
   },
   {
      category: 'groupII',
      name: 'someValue2'
   },
   {
      category: 'groupII',
      name: 'someValue3'
   },
   {
      category: 'groupII',
      name: 'someValue4'
   },
   {
      category: 'groupIII',
      name: 'someValue1'
   },
   {
      category: 'groupIII',
      name: 'someValue2'
   },
   {
      category: 'groupIII',
      name: 'someValue3'
   },
   {
      category: 'groupIII',
      name: 'someValue4'
   }
];
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since you didn't provide examples of those two arrays you need to combine, I will make assumption about their structures and give you one example of how you should do it:

 const groups = [
  {
    id: 1,
    name: 'GroupI'
  }, {
    id: 2,
    name: 'GroupII',
  }, {
    id: 3,
    name: 'GroupIII',
  }
];

const data = [
  { category: 'groupI', name: 'someValue1' },
  { category: 'groupI', name: 'someValue2' },
  { category: 'groupI', name: 'someValue3' },
  { category: 'groupI', name: 'someValue4' },
  { category: 'groupII', name: 'someValue1' },
  { category: 'groupII', name: 'someValue2' },
  { category: 'groupII', name: 'someValue3' },
  { category: 'groupII', name: 'someValue4' },
  { category: 'groupIII', name: 'someValue1' },
  { category: 'groupIII', name: 'someValue2' },
  { category: 'groupIII', name: 'someValue3' },
  { category: 'groupIII', name: 'someValue4' }
];

let groupedData = {};

groups.forEach(g => {
  groupedData[g.name] = {
    id: g.id,
    name: g.name,
    array: data.filter(x => x.category.toLowerCase() === g.name.toLowerCase())
  }
});

console.log(groupedData); // This will give you the output you are expecting
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