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

195
Visualizações
How can I group an array?

I have this array :

array=[{x:2,y:test0,z:title},{x:3,y:test0,z:title},{x:4,y:test0,z:title},{x:5,y:test0,z:title2},{x:6,y:test0,z:title2},{x:7,y:test0,z:title2}]

And I turned it into this array this way:

 const groups = array.reduce((acc, row) => {
       acc[row[title]] = acc[row[title]] || [];
        acc[row[title]].push(row);
        return acc;
      }, {});

this is result:

  [
    title:[{x:2,y:test0},{x:3,y:test0},{x:4,y:test0}]
    title2:[{x:5,y:test1},{x:6,y:test1},{x:7,y:test1}]
    ]

And I want to have this array:

[
 title:[test0:[{x:2},{x3:},{x:4}]]
 title2:[test1:[{x:5}],test2:[{x:6}],test3:[{x:7}]]]
]

How can I get to this array?

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

0

You want y to be the key. You can just use object destructuring like:

const {y, ...obj} = row[title];
const fixedRow = {[y]: obj};

In this way y gets removed from the object, and you can use the previous value of y as key and the remaining properties as value.

This won't look exactly like your expected result, because arrays can't have properties, but I suppose it's just a typo.

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