I'm cloning a variable for manipulation in one of my functions, and I'm NOT reassigning it back. Why is the second console.log() printing the below?
Here is the code:
console.log('BEFORE -> ', JSON.parse(JSON.stringify(vm.referenceMatrix)));
var referenceId = null;
var referenceMatrix = vm.referenceMatrix;
for (var i = 0; i < length; i++) {
referenceId = references[i].article_reference_id;
referenceMatrix[referenceId] = i + 1;
}
console.log('AFTER -> ', JSON.parse(JSON.stringify(vm.referenceMatrix)));
//vm.referenceMatrix = referenceMatrix;
As you can see, vm.referenceMatrix = referenceMatrix; is commented out, so how it this possible?
Edit:
This code generates the same output as in the image above:
console.log('BEFORE -> ', JSON.parse(JSON.stringify(vm.referenceMatrix)));
var referenceId = null;
var referenceMatrixs = vm.referenceMatrix;
for (var i = 0; i < length; i++) {
referenceId = references[i].article_reference_id;
referenceMatrixs[referenceId] = i + 1;
}
console.log('AFTER -> ', JSON.parse(JSON.stringify(vm.referenceMatrix)));
//vm.referenceMatrix = referenceMatrix;
