zl程序教程

您现在的位置是:首页 >  后端

当前栏目

用nodejs实现PHP的print_r函数代码

NodejsPHP代码 实现 函数 PRINT
2023-06-13 09:15:19 时间
复制代码代码如下:

functionergodic(obj,indentation){
 varindent=" "+indentation;
 if(obj.constructor==Array||obj.constructor==Object){

   for(varpinobj){
     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+"}");
     }elseif(obj[p].constructor==String){
       console.log(indent+"["+p+"]=>""+obj[p]+""");
     }else{
       console.log(indent+"["+p+"]=>"+obj[p]+"");
     }
   }
 }
}

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

varstu={"name":"Alan","grade":{"Chinese":120,"math":130,"competition":{"NOI":"Firstprize"}}};

print_r(stu);