There are several ways to copy without reference, and . Slice() is one of them. However, I was having the problem that the . Slice() for arrays within arrays will take only the reference instead of actually copying, as shown in the accepted answer to this link’s question:
https://stackoverflow.com/questions/3978492/fastest-way-to-duplicate-an-array-in-javascript-slice-vs-for-loop
"Please note that These methods will clone the Array Object itself, array Contents However are copied by Reference and are not deep cloned."
array1 = [ [ [1], [2], [3] ], [ [4], [5], [6] ] ];
// array "pai", com 2 "filhos", e 6 "netos"
array2 = array1.slice();
// Copia os filhos, mas com os "netos" faz referência
So to solve this, I did the following:
for (var x = 0; x < array1.length; x++) {
array2.push ( [ array1[0][0].slice(), array1[0][1].slice(), array1[0][2].slice() ] )
}
That at the end of the day
array1 === array2
without having any reference.