In a program I'm writing I have a multidimensional array and I need to perform some operation only on the most inner objects.
Is there any better way to write this code?
for (const threeDimensionalArray of fourDimensionalArray) {
for (const twoDimensionalArray of threeDimensionalArray) {
for (const oneDimensionalArray of twoDimensionalArray) {
for (const obj of oneDimensionalArray) {
// do something
}
}
}
}
Something like:
for (const obj someSpecialKeyword fourDimensionalArray) {
// do something
}
Note: I know of one solution which is using a flattening function. The problem with using a flattening function is that this iteration is going to be used a lot in the program so I am worried about the performance. Even if I save this multidimensional array in two formats (a flat one and a multidimensional one) it can cause performance issues because this array is also changed a lot and also makes the code longer.
What would be the best approach here?