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

150
Visualizações
array data changed without manipulation

I face an behavior which I cant explain but reproduce. I created an array of objects as a source data. I declared an second array and passing the data from the first. Now I can manipulate the second array without losing the original data.

But as soon as I manipulate the second array the source array changed too.

Please refer to the console output.

  • The first console.log(person) before maniupulation of filterPerson (expected output ok).
  • The second console.log(person) after maniupulation of filterPerson (expected output not ok) Since I did not adressed the first array those data shouldn´t be touched at all.

Can somebody explain me this?

var person = [{"name": "Abby", "age": "22"}, {"name": "Paul", "age": "28"}, {"name": "Susi", "age": "19"} ]
var filterPerson = []

filterPerson = person

console.log(person)

for (var element of filterPerson) {
    if (element.age != 22) {
        element.age = 18
  }
}

console.log(person)

console.log(filterPerson)

about 4 years ago · Juan Pablo Isaza
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