Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Uzwername/a68983b746f4339267f395660a4e5722 to your computer and use it in GitHub Desktop.
Save Uzwername/a68983b746f4339267f395660a4e5722 to your computer and use it in GitHub Desktop.
// 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