How do I display the content of a JavaScript object in a string format like when we alert
a variable?
The same formatted way I want to display an object.
How do I display the content of a JavaScript object in a string format like when we alert
a variable?
The same formatted way I want to display an object.
Here's function.
It can show object using tab indent with readability.
Use this:
I needed a way to recursively print the object, which pagewil's answer provided (Thanks!). I updated it a little bit to include a way to print up to a certain level, and to add spacing so that it is properly indented based on the current level that we are in so that it is more readable.
Usage:
In NodeJS you can print an object by using
util.inspect(obj)
. Be sure to state the depth or you'll only have a shallow print of the object.Function:
Usage:
Example:
http://jsfiddle.net/WilsonPage/6eqMn/
If you would like to print the object of its full length, can use
If you want to print the object by converting it to the string then