Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //your object
- var o = {
- foo:"bar",
- arr:[1,2,3],
- subo: {
- foo2:"bar2"
- }
- };
- //called with every property and it's value
- function process(key,value) {
- log(key + " : "+value);
- }
- function traverse(o,func) {
- for (i in o) {
- func.apply(this,[i,o[i]]);
- if (typeof(o[i])=="object") {
- //going on step down in the object tree!!
- traverse(o[i],func);
- }
- }
- //that's all... no magic, no bloated framework
- traverse(o,process);
- function traverse(jsonObj) {
- if( typeof jsonObj == "object" ) {
- $.each(jsonObj, function(k,v) {
- // k is either an array index or object key
- traverse(v);
- }
- }
- else {
- // jsonOb is a number or string
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement