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

399
Visualizações
Merge objects into array

I want to convert multiple objects with same id as test to array of objects

Actual:

const array= [
    { "test": 1},
    { "test": 2},
 { "test": 3},
 { "test": 4},
]

Expected:

test: [1,2,3,4]

Can someone please help

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

0

Just use the native method map (read more here or here) like this:

const array= [
  { "test": 1},
  { "test": 2},
  { "test": 3},
  { "test": 4},
];
const newArray = array.map(p => p.test);
console.log(JSON.stringify(newArray)); //[1,2,3,4]

Hope this helps.. ;D

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the map function for this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map :

const newarray = array.map(x => x.test);
console.log(newarray);
about 4 years ago · Juan Pablo Isaza Relatório

0

const result = {};

yourArray.forEach( ( object ) => {
  const keys = Object.keys( object );
  for ( let i = 0; i < keys.length; i++ ) {
     const key = keys[ i ];
     if ( ! key in result ) { 
        result[ key ] = [];
     }
     result[key] = [...result[key], ...object[key] ];
  }
});

console.log( result );

In this case:

  1. You need to iterate in your array
  2. On each iteration you have a new object
  3. From there you iterate in all the keys of that current object
  4. If the key does not exists in your result create a new empty array
  5. Merge the value from the result with the current value of the object.
  6. Values are stored in result.
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