Created
March 21, 2021 02:46
-
-
Save Uzwername/a68983b746f4339267f395660a4e5722 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetched from server | |
const nestedNumbers = [ | |
[[0], [[[[[[[1, 2]]]]]]], [3]], | |
[[[4], [[5]]], [[[6, 7, 8]]]], | |
[9] | |
]; | |
const incrementNestedNumbers = (arrayWithNums) => { | |
for (let i = 0; i < arrayWithNums.length; i++) { | |
if (Array.isArray(arrayWithNums[i])) { // if array | |
incrementNestedNumbers(arrayWithNums[i]); | |
} else { // if number | |
arrayWithNums[i] = arrayWithNums[i] + 1; | |
} | |
} | |
}; | |
incrementNestedNumbers(nestedNumbers); | |
/* nestedNumbers now look like this: | |
[[1], [[[[[[[2, 3]]]]]]], [4]], | |
[[[5], [[6]]], [[[7, 8, 9]]]], | |
[10] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment