This question has been asked here and more importantly here. I tried all the answers and none solved my issue. It's very basic.
I have the following array. My various attempts at copying do not persist after splicing, or performing any destructive action. Even the copies are mutated. How can I copy this so the original version persists?
var arrs = [ [ 3 ], [ 7, 4 ], [ 2, 4, 6 ], [ 8, 5, 9, 3 ] ]
var arrsCopy1 = arrs.slice()
var arrsCopy2 = arrs.map(arr => {
return arr
})
var arrsCopy3 = [...arrs]
//test one of the copies
arrsCopy3.forEach(arr => {
return arr.splice(0, arr.length)
})
arrs, arrsCopy1, arrsCopy2, arrsCopy3 => [[],[],[],[]]
Here is a fiddle that demos my problem (using reverse()
instead of splice
)