Lets say I have a Multi-Dimensional array as per the following code:
var theArray = [
[1, 2, 3],
[3, 2, 1],
[1, 2, 3],
[1, 2, 1]
]
I need to perform an iteration where I mutate the digits based on some conditions:
- If digit 1 has a 3 below it; it transforms to "foo".
- If digit 3 has a 1 above it; it transforms to "bar".
- If a vertical consecutive sequence of 2 pairs of 1 & 3 or 3 & 1 occurs, no mutation occurs to the pairs.
My issue is that when I iterate through it using forEach(), I end up mutating the digits in the current array before i get to the next array, and so I invalidate the conditions because once the iteration goes to the next array; the checks fail because there will be "foo" or "bar" for example instead of 1 or 2.
So something like this isn't going to work:
someArray.forEach((array, index) => {
array.forEach((digit, numIndex) => {
if(someArray[index + 1][numIndex] == 3 && digit == 1) {
array[numIndex] = "foo"
} else if(someArray[index - 1][numIndex] == 1 && digit == 3) {
array[numIndex] = "bar"
}
})
})
Therefore; I presume there has to be a way to "live check" if these conditions exist through all arrays at the same time. Especially taking into consideration the last constriction which would need to check through vertical pairs.
Expected result:
var theArray = [
["foo", 2, 3],
["bar" , 2, 1],
[1, 2, 3]
[1, 2, 1]
]
How is this achieved ?
Thanks in advance for the help!
returna value fromArray.prototype.forEach()[ [ "foo", 2, 3 ], [ 3, 2, "foo" ], [ 1, 2, 3 ], [ 1, 2, 1 ] ]?