Last active
August 29, 2015 14:16
-
-
Save piscis/eaec26b3ea6b0a6fa94a to your computer and use it in GitHub Desktop.
Traverse JSON Object
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
var obj = { | |
a:1, | |
b: { | |
c:2 | |
}, | |
d: [1,2,3,4] | |
}; | |
function process(key,value) { | |
console.log(key,value) | |
} | |
function traverse(o,func) { | |
for (var i in o) { | |
func.apply(this,[i,o[i]]); | |
if (o[i] !== null && typeof(o[i])=="object") { | |
traverse(o[i],func); | |
} | |
} | |
} | |
traverse(obj,process); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment