can someone explain the reason for the behavior below?
In scenario 1 if I alter an element of the shallow copy of the array, the original array is affected.
In scenario 2 if I alter the shallow copy of the array directly by just assigning an empty array to it, it doesn't affect the original array.
What am I missing here? All I want is scenario1 to NOT alter the original array. Cant' really find an answer on this...
//scenario 1
var peopleOriginal =[
{name: 'alex', age:999 }
]
var peopleCopy = peopleOriginal.slice()
var updatedPerson = peopleCopy[0];
updatedPerson.name = 'bob';
console.log(peopleOriginal) // {name: 'bob', age:999 }
// scenario 2
var peopleOriginal2 =[
{name: 'alex', age:999 }
]
var peopleCopy2 = peopleOriginal2.slice()
peopleCopy2 = []
console.log(peopleOriginal2) // {name: 'alex', age:999 }