we have two same size 2D array.
var firstArray = [
['-', '-', '-'],
['-', 'X', '-'],
['-', '-', 'O']
]
var secondArray = [
['-', '-', '-'],
['-', 'X', '-'],
['-', '-', '-']
]
after replacing firstArray value with secondArray value, firstArray should be like this
firstArray = [
['-', '-', '-'],
['-', 'X', '-'],
['-', '-', '-']
]
I saw this question , but the solution is for flat arrays and I dont want to convert my 2D arrays to flat.
3
Answers
One way to do it is to use nested for loops. Try the following code:
Let me know if this helps.
To replace the values of one 2D array with another 2D array without converting them to flat arrays, you can use nested loops to iterate over each element and assign the corresponding value from the second array to the first array.
Here is my tested code.
for identical dimensional arrays you can try spread operator like:
or or arrays of not identical dimensions you can apply loops or you also use function. using function output would create new reference. So that first array would unchanged.