Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!