Let's say I have this code:
var arr = [1, 2, 3, 4, 5];
var arr1 = arr;
arr.splice(Math.min(...arr), 1);
But for some reason, the value of arr is the same as arr1. What if I want arr to be different than arr1 and be independent of its updating?
Let's say I have this code:
var arr = [1, 2, 3, 4, 5];
var arr1 = arr;
arr.splice(Math.min(...arr), 1);
But for some reason, the value of arr is the same as arr1. What if I want arr to be different than arr1 and be independent of its updating?
Your issue is this line:
var arr1 = arr;
According to the situation you describe you want to copy the array.
But with this line you're just assigning it to another variable instead of copying.
So in fact, without copying, the arrays are the same.
In order to copy just do something like this:
let copy = array.slice(0);
/* or (will not work for large arrays) */
let copy = [...array];
This should do the trick for you
var arr = [1, 2, 3, 4, 5];
var arr1 = arr.slice();
arr.splice(Math.min(...arr), 1);
console.log(arr);
console.log(arr1);
Arrays are objects, so you can copy them the same way
var x = [1, 2, 3],
y = Object.assign([], x);
x[0] = 16;
console.log(x);
console.log(y);