Home > Web Front-end > JS Tutorial > body text

Use nodejs to implement PHP's print_r function code_javascript skills

WBOY
Release: 2016-05-16 16:55:43
Original
1311 people have browsed it
复制代码 代码如下:

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);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template