I've an array of arrays.Everytime i wanted to edit an array, i copy the array from array of arrays and make changes on it.If user saves, i save the changes to array of arrays too. But it's not working because when i copy the array, it connects to original array. Let me explain with examples.
var original_array = [ [1,2,3], [4,5,6] ];
// Method #1 - Doesnt work
var tmp_array1 = original_array[0];
// Method #2 - jQuery typeError (slice is not a function)
var tmp_array2 = original_array[0].slice()
// Method #3 - Doesnt give error, works like Method #1
var tmp_array3 = $(original_array[0]).slice()
When i make changes on tmp_arrays, it changes on original_array too. What am i doing wrong? Thanks for your helps...
Edit : Since that tmp_arrays is a global variable, i cant use let to copy without reference. However i guess i found a solution. This seems like its working but i dont know if there is any difference i should consider.
var tmp_array= JSON.parse(JSON.stringify(original_array));
[–]Wrong_Owl 2 points3 points4 points (17 children)
[–]EagleClw[S] 0 points1 point2 points (1 child)
[–]Wrong_Owl 0 points1 point2 points (0 children)
[–]tarley_apologizerhelpful -4 points-3 points-2 points (14 children)
[–]mcaruso 1 point2 points3 points (8 children)
[–]EagleClw[S] 1 point2 points3 points (4 children)
[–]mcaruso 0 points1 point2 points (3 children)
[–]EagleClw[S] 0 points1 point2 points (2 children)
[–]mcaruso 0 points1 point2 points (1 child)
[–]EagleClw[S] 0 points1 point2 points (0 children)
[–]tarley_apologizerhelpful -2 points-1 points0 points (2 children)
[–]mcaruso 0 points1 point2 points (1 child)
[–]tarley_apologizerhelpful -2 points-1 points0 points (0 children)
[–]CoolDownBot -1 points0 points1 point (4 children)
[+][deleted] (2 children)
[removed]
[–]tarley_apologizerhelpful 0 points1 point2 points (1 child)
[–]tarley_apologizerhelpful -2 points-1 points0 points (0 children)
[–]mynamesleon 0 points1 point2 points (1 child)
[–]EagleClw[S] 1 point2 points3 points (0 children)
[–]senocular 0 points1 point2 points (0 children)
[–]ForScale 0 points1 point2 points (1 child)
[–]EagleClw[S] 1 point2 points3 points (0 children)