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

106
Visualizações
How can I assign array values into object keys for other object?

So I have an array of objects [{}, {}, {}] and simple array [true, false, true]

Is there any way to make first array [{value: true}, {value: false}, {value: true}]?

I mapped over the object array

const a = [{a: 1}, {a: 2}, {a: 3}] const b = [true, false, true]  const c = a.map((item) => {...item, value: }) 

I don't understand how to assign the value from the second array.

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

0

Assuming your array of objects is called objects and your simple array is called array, then you can execute:

objects.forEach((o, i) => o['value'] = array[i])
about 4 years ago · Juan Pablo Isaza Relatório

0

You can map each element of the array to the object:

console.log([true, false, true].map(el => ({ value: el })));

or if you want to overwrite the elements in the first array

const arr = [{}, {}, {}];
const arr2 = [true, false, true];
arr.forEach((el, idx) => el.value = arr2[idx]);

console.log(arr);

about 4 years ago · Juan Pablo Isaza Relatório

0

An alternative way of accomplishing this is by using the unshift() method which will add the new elements to the beggining of the array.

var arr1 = [true, false, true], arr2 = [{}, {}, {}];
for(el of arr1){ arr2.unshift({'value': el}) }
console.log(arr2);

If you want to loose the last three empty objects in the array, slice them like so: arr2.slice(0,3)

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