I'm trying to write a function that picks a random entry from a multidimensional array (myArray), saves it to a new variable (myTempArray) and updates the first entry with an id number (counter).
When I change the value of mytempArray[0][0][0] it updates the equivalent value in myArray as well. I don't want these values to change.
How do I stop this from happening? Is slice the wrong method to use? If so, how do I duplicate the original array entry so I can modify it and use elsewhere? Code is below:
var myArray = [
[[0, "Description 1", 0, 0, 1, 6, 0, 0, 0, true, 0]],
[[0, "Description 2", 0, 0, 1, 6, 0, 0, 0, true, 0]],
[[0, "Description 3", 0, 0, 1, 6, 0, 0, 0, true, 0]],
];
function randomNumber (min, max) {
return Math.floor((Math.random() * max) + min);
};
counter = 1;
function myfunction(){
myrand = randomNumber(0,myArray.length-1);
mytempArray = myArray.slice(myrand,myrand+1);
mytempArray[0][0][0] = counter;
counter++;
};
myfunction();