0

I'm trying to use a for loop to modify an array then store that modification in another array than do another modification and store that to the second array etc, but my second array is just d instances of the last iteration of my for loop. This seems like a closure problem but I can't figure out a workaround?

for (var d=1; d<arr1.length; d++) {
       remove = arr1.splice(d,1).toString(); 
        arr1.splice(d+1,0,remove);
     arr2.push(arr1);
   }
    return arr2;
Keli
  • 63
  • 2
  • 8

1 Answers1

0

If you want to store all modified versions of your array, I suggest using an array of arrays, in this case let's call that top-level array master. Each time you modify your array, you would just push it to the master array.

This would give you what is called a "2d array". Then you would be able to access any version of your array that you wanted to. Your first array would be master[0], second would be master[1], etc. To access the first element of your second array, you can then just say master[1][0].

Hope this helps!

minorcaseDev
  • 181
  • 5