I have an array of objects like this:
var arr = [{'name':'one','age':24},{'name':'two','age':21}]
Then I assigned this array to another variable var newVar = arr
. Next, I modified the value of an object in newVar
using newVar[0].age = 18
. However, this change also affected the original data stored in arr
. The updated array became:
var arr = [{'name':'one','age':18},{'name':'two','age':21}]
I would like the original data to remain unchanged when modifying values in a different variable that holds the same data. How can I achieve this in JavaScript?