node console.log / util.inspect for nested Object / Array

前端 未结 2 934
北恋
北恋 2021-01-11 17:47

For the nested Object or Array

var obj = {
  foo:
  {
    foo:
    {
      foo:
      {
        foo:
        {
          foo:
          {
            foo: \'         


        
相关标签:
2条回答
  • 2021-01-11 18:00

    Use JSON.stringify:

    var obj = {
      foo:
      {
        foo:
        {
          foo:
          {
            foo:
            {
              foo:
              {
                foo: 'foo'
              }
            }
          }
        }
      }
    };
    console.log(JSON.stringify(obj));
    

    Result:

    {"foo":{"foo":{"foo":{"foo":{"foo":{"foo":"foo"}}}}}}
    
    0 讨论(0)
  • 2021-01-11 18:22

    util.inspect() takes a second options argument, where you can specify depth. The default is 2.

    http://nodejs.org/api/util.html#util_util_inspect_object_options

    So:

    util = require('util');
    var obj = { foo: { foo: { foo: { foo: { foo: { foo: 'foo' } } } } } };
    console.log(util.inspect(obj, {depth:12}));
    

    ... yields:

    { foo: { foo: { foo: { foo: { foo: { foo: 'foo' } } } } } }
    
    0 讨论(0)
提交回复
热议问题