I've got a javascript object which has been JSON parsed using JSON.parse
I now want to print the object so I can debug it (something is going wrong with the function). When I do the following...
for (property in obj) {
output += property + ': ' + obj[property]+'; ';
}
console.log(output);
I get multiple [object Object]'s listed. I'm wondering how would I print this in order to view the contents?
try
console.dir()
instead ofconsole.log()
MDN says
console.dir()
is supported by:If you want to debug why not use console debug
Just use
and you will get this in chrome console :
Most debugger consoles support displaying objects directly. Just use
Depending on your debugger this most likely will display the object in the console as a collapsed tree. You can open the tree and inspect the object.
If you want a pretty, multiline JSON with indentation then you can use
JSON.stringify
with its 3rd argument:For example:
or
will give you following result:
In a browser
console.log(obj)
does even better job, but in a shell console (node.js) it doesn't.You know what JSON stands for? JavaScript Object Notation. It makes a pretty good format for objects.
JSON.stringify(obj)
will give you back a string representation of the object.