How can I view the structure of an array in javascript using alert()
?
A very basic approach is |
|||
|
EDIT: Firefox and Google Chrome now have a built-in I tend to use the jQuery-json plugin as follows:
This prints the array in a format like
However, for debugging your Javascript code, I highly recommend Firebug It actually comes with a Javascript console, so you can type out Javascript code for any page and see the results. Things like arrays are already printed in the human-readable form used above. Firebug also has a debugger, as well as screens for helping you view and debug your HTML and CSS. |
||||
|
pass your js array to the function below and it will do the same as php print_r() function
|
|||||||
|
You can use |
|||||
|
If this is for debugging purposes, I would advise you use a JavaScript debugger such as Firebug. It will let you view the entire contents of arrays and much more, including modifying array entries and stepping through code. |
|||
|
I'd recommend using toString(). Ex. |
|||
|
You could write a function that will convert and format this array as string. Even better: use FireBug for debugging instead of alerts. |
|||
|
Better use Firebug (chrome console etc) and use console.dir() |
|||
|
|
||||
|
protected by Quentin Jul 18 at 21:43
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.
console.log
-- it's great for introspection of JavaScript objects. – Deniz Dogan Jun 9 '10 at 14:15console.debug
would actually work better. – Matt Ball Jun 9 '10 at 14:25