Skip to content Skip to sidebar Skip to footer

Node Console.log / Util.inspect For Nested Object / Array

For the nested Object or Array var obj = { foo: { foo: { foo: { foo: { foo: { foo: 'foo' }

Solution 1:

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');varobj= { foo: { foo: { foo: { foo: { foo: { foo:'foo' } } } } } };console.log(util.inspect(obj, {depth:12}));

... yields:

{ foo: { foo: { foo: { foo: { foo: { foo: 'foo' } } } } } }

Solution 2:

Use JSON.stringify:

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

Result:

{"foo":{"foo":{"foo":{"foo":{"foo":{"foo":"foo"}}}}}}

Post a Comment for "Node Console.log / Util.inspect For Nested Object / Array"