Skip to content

Instantly share code, notes, and snippets.

@chrisjlee
Last active November 11, 2015 03:57
Show Gist options
  • Save chrisjlee/babf57d37cfc2c46a774 to your computer and use it in GitHub Desktop.
Save chrisjlee/babf57d37cfc2c46a774 to your computer and use it in GitHub Desktop.
function traverse(x, level) {
if (isArray(x)) {
traverseArray(x, level);
} else if ((typeof x === 'object') && (x !== null)) {
traverseObject(x, level);
} else {
console.log(level + x);
}
}
function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
function traverseArray(arr, level) {
console.log(level + "<array>");
arr.forEach(function(x) {
traverse(x, level + " ");
});
}
function traverseObject(obj, level) {
console.log(level + "<object>");
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(level + " " + key + ":");
traverse(obj[key], level + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment