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.
If you're looking for something that can return a prettified string of any javascript object, check out https://github.com/fresheneesz/beautinator . An example:
Results in:
It even works if there are functions in your object.
Javascript Function
Printing Object
via print_r in Javascript
To print the full object with Node.js with colors as a bonus:
Colors are of course optional, 'depth: null' will print the full object.
The options don't seem to be supported in browsers.
References:
https://developer.mozilla.org/en-US/docs/Web/API/Console/dir
https://nodejs.org/api/console.html#console_console_dir_obj_options
With Firefox
If you want to print the object for debugging purposes, I suggest instead installing Firebug for Firefox and using the code:
With Chrome
will display
Note : you must only log the object. For example this won't work :
As it was said before best and most simply way i found was
(This has been added to my library at GitHub)
Reinventing the wheel here! None of these solutions worked for my situation. So, I quickly doctored up pagewil's answer. This one is not for printing to screen (via console, or textfield or whatever). It is, however, for data transport. This version seems to return a very similar result as
toSource()
. I've not triedJSON.stringify
, but I assume this is about the same thing. The result of this function is a valid Javascript object declaration.I wouldn't doubt if something like this was already on SO somewhere, but it was just shorter to make it than to spend a while searching past answers. And since this question was my top hit on google when I started searching about this; I figured putting it here might help others.
Anyhow, the result from this function will be a string representation of your object, even if your object has embedded objects and arrays, and even if those objects or arrays have even further embedded objects and arrays. (I heard you like to drink? So, I pimped your car with a cooler. And then, I pimped your cooler with a cooler. So, your cooler can drink, while your being cool.)
Arrays are stored with
[]
instead of{}
and thus dont have key/value pairs, just values. Like regular arrays. Therefore, they get created like arrays do.Also, all string (including key names) are quoted, this is not necessary unless those strings have special characters (like a space or a slash). But, I didn't feel like detecting this just to remove some quotes that would otherwise still work fine.
This resulting string can then be used with
eval
or just dumping it into a var thru string manipulation. Thus, re-creating your object again, from text.Let me know if I messed it all up, works fine in my testing. Also, the only way I could think of to detect type
array
was to check for the presence oflength
. Because Javascript really stores arrays as objects, I cant actually check for typearray
(there is no such type!). If anyone else knows a better way, I would love to hear it. Because, if your object also has a property namedlength
then this function will mistakenly treat it as an array.EDIT: Added check for null valued objects. Thanks Brock Adams
EDIT: Below is the fixed function to be able to print infinitely recursive objects. This does not print the same as
toSource
from FF becausetoSource
will print the infinite recursion one time, where as, this function will kill it immediately. This function runs slower than the one above, so I'm adding it here instead of editing the above function, as its only needed if you plan to pass objects that link back to themselves, somewhere.Test:
Result:
NOTE: Trying to print
document.body
is a terrible example. For one, FF just prints an empty object string when usingtoSource
. And when using the function above, FF crashes onSecurityError: The operation is insecure.
. And Chrome will crash onUncaught RangeError: Maximum call stack size exceeded
. Clearly,document.body
was not meant to be converted to string. Because its either too large, or against security policy to access certain properties. Unless, I messed something up here, do tell!