Created
July 5, 2016 14:08
-
-
Save mir4ef/3ce19d883a67b46f6d615521b885ff1d to your computer and use it in GitHub Desktop.
Recursive loop over multi dimensional arrays in JavaScript
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
// code from | |
// http://stackoverflow.com/questions/15854425/iterate-over-a-javascript-array-without-using-nested-for-loops/15854485#15854485 | |
function printArray(arr) { | |
for (var i = 0; i < arr.length; i++) { | |
if (Array.isArray(arr[i])) { | |
printArray(arr[i]); | |
} else { | |
console.log(arr[i]); | |
} | |
} | |
} | |
// polyfill | |
if (!Array.isArray) { | |
Array.isArray = function(o) { | |
return !!o && Object.prototype.toString.call(o) === "[object Array]" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment