I do understand there are other ways of doing this, so I don’t need a different solution, I just want to understand why this happens!
While working on something else I came across an issue addressing arrays from a function which I don’t understand, so I made a simple version to look at the issue.
const origionalArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const removeElementByIndex = function(ind, arr) {
let j = 0, tmpArr = [];
for (let i=0; i<arr.length; i++) {
if (i !== ind) {
tmpArr[j] = arr[i];
j++;
};
};
arr.splice(0, arr.length);
console.log(origionalArray, arr, tmpArr);
arr = [...tmpArr];
console.log(origionalArray, arr, tmpArr);
};
removeElementByIndex(4, origionalArray);
As you can see the funciton takes two arguments, the name of the array and the index of an element to be removed. (Yes, I do know arrayName.splice(index, 1);
will achieve the same result) puts the elements that should remain in the origional array into a temp array, clears the origional array, then attempts to populate the origional array with the values from the temp array:
And here is the problem I don’t understand.
arr.splice(0, arr.length);
does clear the array (both arr withing function and origioanlArray
but
arr = [...tmpArr];
ONLY populates arr within the function! origionalArray is uneffected
why is one line effecting the origionalArray and another not?
It obviously isn’t a scope issue and using splice alone in the fuction instead does work, and that is probably why I am confused.
const origionalArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const removeElementByIndex = function(ind, arr) {
arr.splice(ind, 1);
};
removeElementByIndex(4, origionalArray);
console.log(origionalArray);
I do have ways around the problem as I said, but I was expecting origionalArray to be populated and don’t undestandy why it isn’t occurring.