Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Created June 15, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangjunjun/eb6e1778af97a9033d44 to your computer and use it in GitHub Desktop.
Save yangjunjun/eb6e1778af97a9033d44 to your computer and use it in GitHub Desktop.
递归输出 json 的值
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