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

125
Visualizações
Having trouble understanding why original object is affected after .map within example

I have been having trouble understanding why temp is being affected here. When looking at the console when running this code:

const objectsArray = [
    {'id': 1, 'name': 'Erik', 'yearsCompleted': 2, 'status': 'student'}, 
    {'id': 2, 'name': 'Carol', 'yearsCompleted': 1, 'status': 'student'}, 
    {'id': 3, 'name': 'Sarah', 'yearsCompleted': 4, 'status': 'student'}
];

temp = {
    actions: objectsArray.map(student => {
    if (student.yearsCompleted === 4) student.status = 'wow';
    return student
    })
}
console.log(temp)

temp2 = {
    actions: objectsArray.map(student => {
    if (student.yearsCompleted === 4) student.status = 'foo';
    return student
    })
}

console.log(temp)
console.log(temp2)

I end up getting the following result:

{ actions:
   [ { id: 1, name: 'Erik', yearsCompleted: 2, status: 'student' },
     { id: 2, name: 'Carol', yearsCompleted: 1, status: 'student' },
     { id: 3, name: 'Sarah', yearsCompleted: 4, status: 'wow' } ] }
{ actions:
   [ { id: 1, name: 'Erik', yearsCompleted: 2, status: 'student' },
     { id: 2, name: 'Carol', yearsCompleted: 1, status: 'student' },
     { id: 3, name: 'Sarah', yearsCompleted: 4, status: 'foo' } ] }
{ actions:
   [ { id: 1, name: 'Erik', yearsCompleted: 2, status: 'student' },
     { id: 2, name: 'Carol', yearsCompleted: 1, status: 'student' },
     { id: 3, name: 'Sarah', yearsCompleted: 4, status: 'foo' } ] }

Why is temp in this case being affected? Are we getting a reference to the objectsArray when setting the actions property of temp to the objectsArray.map function? I thought temp wouldn't be affected in this case. Thank you.

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

0

That is because student.status = 'foo' changes the actual item in the temp array. You need to make a copy and return it using the spread operator(...) as below.

const objectsArray = [{
    'id': 1,
    'name': 'Erik',
    'yearsCompleted': 2,
    'status': 'student'
  },
  {
    'id': 2,
    'name': 'Carol',
    'yearsCompleted': 1,
    'status': 'student'
  },
  {
    'id': 3,
    'name': 'Sarah',
    'yearsCompleted': 4,
    'status': 'student'
  }
];

temp = {
  actions: objectsArray.map(student => {
    if (student.yearsCompleted === 4) {
      return { ...student,
        status: 'wow'
      }
    };
    return student
  })
}
console.log(temp)

temp2 = {
  actions: objectsArray.map(student => {
    if (student.yearsCompleted === 4) {
      return { ...student,
        status: 'foo'
      }
    };
    return student
  })
}

console.log(temp)
console.log(temp2)

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