Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  82] [ 7]  / answers: 1 / hits: 6963  / 11 Years ago, mon, february 3, 2014, 12:00:00

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!


More From » node.js

 Answers
2

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' } } } } } }

[#48118] Sunday, February 2, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lailab

Total Points: 706
Total Questions: 102
Total Answers: 95

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;