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

262
Visualizações
How to group objects with the same value into a bigger object?

I have data in the following format:

data = [
 { group: G1, name: A},
 { group: G1, name: B},
 { group: G2, name: C},
 { group: G2, name: D}
]

and try to turn it into data in the format of:

 transformedData = [
  {group: G1, names: ['A','B']},
  {group: G2, names: ['C','D']}
]

I have no idea how to do it in JavaScript. Any help appreciated.

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

0

Presented below is one possible way to achieve the desired objective.

Code Snippet

const myTransform = arr => (
  Object.values(
    arr.reduce(
      (acc, { group, name }) => (
        (
          acc[group] ??= ({
            group,
            names: []
          })
        ).names.push(name),
        acc
      ),
      {}
    )
  )
);

/* explanation
// method to transform array
const myTransform = arr => (
  Object.values(        // extract values from the result of below operation
    arr.reduce(         // iterate over "arr" using ".reduce()"
      (acc, { group, name }) => {     // "acc" is accumulator, destructure group, name
        // set-up key-value pair in "acc" for group with "names" array as part of value
        (acc[group] ??= ({ group, names: []}));
        // push the "name" into the "names" array
        acc[group].names.push(name);
        return acc;             // return "acc"
      },
      {}
    )
  )
);
*/

const data = [
 { group: 'G1', name: 'A'},
 { group: 'G1', name: 'B'},
 { group: 'G2', name: 'C'},
 { group: 'G2', name: 'D'}
];

console.log(
  'transformed array:\n',
  myTransform(data)
);
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

Comments added to the snippet above.

about 4 years ago · Juan Pablo Isaza Relatório

0

Be careful withe the declaration of data, otherwise you would get G1 not defined error.

Use Array#reduce and Object.values methods as follows:

const input = [{ group: "G1", name: "A"}, { group: "G1", name: "B"}, { group: "G2", name: "C"}, { group: "G2", name: "D"}],

      output = Object.values(
          input.reduce(
              (prev, {group,name}) =>
              prev[group] ? 
              ({...prev,[group]:{group,name:[...prev[group].name,name]}}) :
              ({...prev,[group]:{group,name:[name]}}), {}
          )
      );
      
console.log( output );

Alternatively ...

const input = [{ group: "G1", name: "A"}, { group: "G1", name: "B"}, { group: "G2", name: "C"}, { group: "G2", name: "D"}],

      output = input.reduce(
          (prev, {group,name}) => {
              const found = prev.find(item => item.group === group);
              if( found ) {
                  found.name.push( name );
              } else {
                  prev = [...prev, {group,name:[name]}];
              }
              return prev;
          }, []
      );
      
console.log( output );

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