Last active
December 7, 2017 04:03
-
-
Save maxtortime/dea7558069b2c6c93f5df691f0ce810c to your computer and use it in GitHub Desktop.
Simple DFS (if it is plain js object)
This file contains hidden or 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
const root = {}; | |
const visit = []; | |
let vn = 0; | |
function dfs(v) { | |
Object.keys(v).forEach(function (k) { | |
if (!visit[vn++]) { | |
if (typeof(v[k]) === 'object') { | |
console.log(k, typeof(k)); | |
dfs(v[k]); | |
} | |
else | |
console.log(k, v[k]); | |
} | |
else visit.splice(vn, 0, true); | |
}); | |
} | |
dfs(root); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ES6 version