Yes I just consoled it, i see…
so it only mutates the specific array like arr[i]. shouldn’t it touch since arr[i] is part of arr?
You have no syntax that says ‘remove’ in your code.
My question is if I mutate the subarray, shouldn’t that subarray mutate the original one? ( I see now it’s not, but just asking).
If you mutate the subarray, the change will be reflected in arr
, but you can’t change the subarray into a single value via the reference to the subarray.
does that mean from just looping? I’m a bit confused sorry.
const myArray = [1, 2, [3, [4]]];
let subArray = myArray[2];
subArray[1] = subArray[1][0];
console.log(myArray); // still have a nested array
You can’t make a nested subarray non-nested by reference, you can only mutate the contents of the nested subarray.
const myArray = [1, 2, [3, [4]]];
let subArray = myArray[2];
let newArr = [];
newArr.push(myArray[0])
console.log(newArr)
console.log(myArray); // still have a nested array
I see even this isn’t mutating it, but I am sorry again shouldn’t push command mutate it?
push mutates the destination, not the origin
Ah I thought it should change the origin.
Okay Thanks I’ll try to solve with your tips
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.