I have a collection of objects called the response and I am creating another variable called object
that's an empty object and creating object.array
and set it to the response variable.
I would think I am creating a new scope. However, if I set the age inside object.array
as null, this sets the age in my response array to null
.
Why is this happening and how can I create a duplicate variable that doesn't affect the original? I need to keep the above variables as is. So object needs to be an object and I need to create an array within which is set to the response and it needs to be inside a for loop
.
This is my code:
function runThisLoop () {
var response = [{
name: 'Name A',
age: 2
},
{
name: 'Name B',
age: 7
}]
var object = {}
object.array = response
for (var val of object.array) {
val.age = null
}
console.log("response", response)
console.log("object.array", object.array)
}
runThisLoop()