Skip to content

Instantly share code, notes, and snippets.

@mir4ef
Created July 5, 2016 14:08
Show Gist options
  • Save mir4ef/3ce19d883a67b46f6d615521b885ff1d to your computer and use it in GitHub Desktop.
Save mir4ef/3ce19d883a67b46f6d615521b885ff1d to your computer and use it in GitHub Desktop.
Recursive loop over multi dimensional arrays in JavaScript
// 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