12

For the nested Object or Array

var obj = {
  foo:
  {
    foo:
    {
      foo:
      {
        foo:
        {
          foo:
          {
            foo: 'foo'
          }
        }
      }
    }
  }
};
console.log(obj);
util.debug(obj);
util.debug(util.inspect(obj));

The console.log or util.debug + util.inspect

{ foo: { foo: { foo: [Object] } } }
DEBUG: [object Object]
DEBUG: { foo: { foo: { foo: [Object] } } }

Under a certain depth, it shows just [Object] with no further detail.

This is always annoying on debugging since I can't investigate the actual value.

Why does node (or V8) implements like this? and Is there any work-around?

Thanks.


EDIT: I received a suggestion

console.log(JSON.stringify(obj));

The result

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

It sort of works, but obviously the whole is stringifyed, and probably I couldn't do this for all object output on debugging. Not generic method.


EDIT2:

solution:

console.log(util.inspect(obj,{ depth: null }));

output:

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

cool. This is what I want!

2 Answers2

17

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' } } } } } }
Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54
2

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"}}}}}}
Alex
  • 11,115
  • 12
  • 51
  • 64