• Register
100 points

This is a way of creating an immutable copy of a 2 dimensional array in js if you want to mutate the second array without affecting the original.

Steps (assuming you already have a “board[]” array created)

// Create original array
let board = [[], [], [], [], [], []];

// Get copy of original array
let c0 = board[0].slice();
let c1 = board[1].slice();
let c2 = board[2].slice();
let c3 = board[3].slice();
let c4 = board[4].slice();
let c5 = board[5].slice();

let boardCopy = [c0.slice(), c1.slice(), c2.slice(), c3.slice(), c4.slice(), c5.slice()];

// Now you can modify "boardCopy" without affecting "board" at all

1. Store each array inside the ‘board’ array into a variable using the “slice()” function.

2. Create another variable “boardCopy” which is an array that stores all the previous variables we created, also use the “slice()” function.

3. Done! “boardCopy” is now a seperate array from “board” but with the same value and won’t affect the first array when mutated. This can also be done with higher dimensional arrays just by using the same logic.

100 points