else {
var reverse = cid.reverse()
var arr = reverse.slice()
for (let i = 0; i < arr.length; i++) {
arr[i][1] = 0
}
console.log(reverse)
When I console log reverse every value is set to “0” which is only what I wanted for the var “arr”. Why is reverse getting mutated even though I used slice?
Ah. What you’re seeing isn’t because of slice. It’s because the array contains arrays. arr is a copy of reverse, but it contains references to the same array objects.
It looks like the only thing that you want from the original array is the names (since you’ll be setting all the values to 0 anyway. Instead of copying the array and then iterating over it to set the values to 0, why not just use the for loop to construct the new array? You can skip the copying altogether.
Hi!
I am also new to this stuff, so better check twice.
I would rather use the filter function on that array.
Or maybe create my own kind of filter function, where I would create a new array from your old one, but only with values, which passed a test.
In your case the test would be: arr[i][1] !== 0.