欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

用nodejs实现PHP的print_r函数代码_javascript技巧

程序员文章站 2022-04-08 22:21:11
...
复制代码 代码如下:

function ergodic(obj,indentation){
var indent = " " + indentation;
if(obj.constructor == Array || obj.constructor == Object){

for(var p in obj){
if(obj[p].constructor == Array|| obj[p].constructor == Object){
console.log(indent + "["+p+"] => "+typeof(obj)+"");
console.log(indent + "{");
ergodic(obj[p], indent);
console.log(indent + "}");
} else if (obj[p].constructor == String) {
console.log(indent + "["+p+"] => '"+obj[p]+"'");
} else {
console.log(indent + "["+p+"] => "+obj[p]+"");
}
}
}
}

function print_r(obj) {
console.log("{")
ergodic(obj, "");
console.log("}")
}

var stu = {'name':'Alan','grade':{'Chinese':120,'math':130,'competition':{'NOI':'First prize'}}};

print_r(stu);