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 in to 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 fuction. It looks somthing 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 be changed by the operations that are performed on the data in func1.