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

130
Visualizações
How can I group an array with nested array?

Does anyone know a simple way to group an array with nested array? For example, i have an array of animals:

[
  ['Cat', 'Dog', 'Monkey'],
  ['my pet', 'it is animals', 'terrestrial animals']
]

I want to make new array with format that's grouped by ['Cat', 'Dog', 'Monkey']:

[
  {
    groupTitle: 'Cat',
    relations: ['my pet', 'it is animals', 'terrestrial animals']
  },
  {
    groupTitle: 'Dog',
    relations: ['my pet', 'it is animals', 'terrestrial animals']
  },
  {
    groupTitle: 'Monkey',
    relations: ['my pet', 'it is animals', 'terrestrial animals']
  }
]

How can i do it? Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Using map()

const arr = [
  ['Cat', 'Dog', 'Monkey'],
  ['my pet', 'it is animals', 'terrestrial animals']
]; // original data
const [groupTitles, relations] = arr; // array destructuring to get individual sub-arrays

const result = groupTitles.map(groupTitle => ({
  groupTitle,
  relations
}));

console.log(result); // print result


I used the following approach to achieve your desired outcome.

  1. Use Array destructuring to get the individual arrays from the original array.
  2. map() through the animals array to get the desired array of objects and store it in a result variable.

Using reduce()

const arr = [
  ['Cat', 'Dog', 'Monkey'],
  ['my pet', 'it is animals', 'terrestrial animals'],
]; // original array

const result = arr[0].reduce((a, b) => {
  return [
    ...a, // previously calculated values
    { // new value
      groupTitle: b,
      relations: arr[1],
    },
  ];
}, []);

console.log(result); // print result

The same result can also be achieved using reduce() method as required.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can make a nested iteration using for loop for example:

const arr = [
  ["Cat", "Dog", "Monkey"],
  ["my pet", "it is animals", "terrestrial animals"],
];

let newArr = [];

for (let i = 0; i <= arr.length; i++) {
  for (let j = 0; j < arr[0].length; j++) {}
  newArr.push({ groupTitle: arr[0][i], relations: arr[1] });
}

console.log(newArr);
about 4 years ago · Juan Pablo Isaza Relatório

0

Using destructuring assignments

const arr = [
  ['Cat', 'Dog', 'Monkey'],
  ['my pet', 'it is animals', 'terrestrial animals']
]

let a,b,c, obj, newArr = [];

[a,b,c] =  arr[1]

arr[0].forEach(el => {

  obj = {
    groupTitle: el,
    relations: [a,b,c]
  }
  newArr.push(obj)
})

console.log(newArr)

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