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
why am I having this output as result?

what would be the result of my code ? I am expecting to have as result one single array of length 1 , I want to output only values of d variable that haven't been updated in my var2 variable but I get an array of length 2 , I want to have output

{name : "david", age : 23, day : 23}

const d = [{name : '',age : '',day :23}]
const var2 =  [...d, { name : 'david', age : 22}]
console.log(var2)

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

0

You are not using the spread operator correctly. You should use it to merge all elements of d[0] with an object containing your updated values.

const d = [{name : '',age : '',day :23}]

// for d containing only one element 
const var2 =  [{...d[0], name : 'david', age : 22}]
// will result in [{name: '', age: '', day: 23, name: 'david', age: 22}] and be merged by keeping the right-most value

// for all elements in d
const var3 = d.map(el => { 
  return {...el, name : 'david', age : 22}
})

console.log(var2)
console.log(var3)

about 4 years ago · Juan Pablo Isaza Relatório

0

Think of it this way:

d is an array of objects

{ name : 'david', age : 22} is an object literal

When you use the spread operator on d, you are telling it to take each object in d and place it into var2, then at the end, append the { name : 'david', age : 22}.

So the result would be an array with all the items you had in d + the object literal { name : 'david', age : 22}.

about 4 years ago · Juan Pablo Isaza Relatório

0

To get your expected result {name : "david", age : 23, day : 23} merge two objects like shown below.

merge Object like {...object1, ...object2}

const d = [{name : '',age : '',day :23}]
const var2 =  {...d[0], ...{ name : 'david', age : 22}}
console.log(var2)

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