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

99
Views
How can I use the same array of objects for 2 functions in javascript

I have an array that looks like this...

const arr = [
    { name: 'Will', text: "Hey, I'm Will" },
    { name: 'Gary', text: "Time to run" },
    { name: 'Bob', text: "Yo it's Bob" }
]

Now I want to run two different functions that both take the same 'arr' variable as an argument. When I pass it into the first function, this function makes changes to each object, adding an additional property to each object. These changes are being carried over to the next function. It looks something like this...

const func1 = data => {
    data.forEach(item => item.year = '2022')

    console.log(data)   
}

const func2 = data => console.log(data)

[
    { name: 'Will', text: "Hey, I'm Will" },
    { name: 'Gary', text: "Time to run" },
    { name: 'Bob', text: "Yo it's Bob" }
]

//  both functions return ...
    // [
    //  { name: 'Will', text: "Hey, I'm Will", year: 2022 },
    //  { name: 'Gary', text: "Time to run", year: 2022 },
    //  { name: 'Bob', text: "Yo it's Bob", year: 2022 }
    // ]

Is there a way that I can prevent the data that is passed into func2 not being changed by the operations that are performed on the data in func1?

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!