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

154
Views
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
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!