-
-
Save yangjunjun/eb6e1778af97a9033d44 to your computer and use it in GitHub Desktop.
递归输出 json 的值
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 data = { | |
"data": { | |
"json1": [ | |
{ | |
"time": "05-17", | |
"msg": 0 | |
}, | |
{ | |
"time": "05-18", | |
"msg": 2 | |
} | |
], | |
"json2": [ | |
{ | |
"time": "05-17", | |
"msg": 0 | |
}, | |
{ | |
"time": "05-18", | |
"msg": 2 | |
} | |
], | |
"json3": [ | |
{ | |
"time": "05-17", | |
"msg": 0 | |
}, | |
{ | |
"time": "05-18", | |
"msg": 2 | |
} | |
] | |
} | |
} | |
function each(data, fn){ | |
if(Object.prototype.toString.call(data) == '[object Array]'){ | |
var l = data.length, i = 0; | |
for(var i = 0, l = data.length; i < l; i++){ | |
each(data[i], fn); | |
} | |
} | |
else if(Object.prototype.toString.call(data) == '[object Object]'){ | |
for(var prop in data){ | |
each(data[prop], fn); | |
} | |
} | |
else { | |
fn(data); | |
} | |
} | |
each(data, function(d){ | |
console.log(d); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment