How can I view the structure of an array in JavaScript using alert()?
11 Answers
A very basic approach is alert(arrayObj.join('\n')), which will display each array element in a row.
1 Comment
alert(myArray.join('\n'));EDIT: Firefox and Google Chrome now have a built-in JSON object, so you can just say alert(JSON.stringify(myArray)) without needing to use a jQuery plugin. This is not part of the Javascript language spec, so you shouldn't rely on the JSON object being present in all browsers, but for debugging purposes it's incredibly useful.
I tend to use the jQuery-json plugin as follows:
alert( $.toJSON(myArray) );
This prints the array in a format like
[5, 6, 7, 11]
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.
Comments
pass your js array to the function below and it will do the same as php print_r() function
alert(print_r(your array)); //call it like this
function print_r(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += print_r(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
1 Comment
You can use alert(arrayObj.toSource());
1 Comment
For readability purposes you can use: alert(JSON.stringify(someArrayOrObj, '', 4));
More about JSON.stringify().
Example:
let user = {
name: 'John',
age: 30,
roles: {
isAdmin: false,
isEditor: true,
},
};
alert(JSON.stringify(user, '', 4));
Output:
{
"name": "John",
"age": 30,
"roles": {
"isAdmin": false,
"isEditor": true
}
}
Comments
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.
Comments
If what you want is to show with an alert() the content of an array of objects, i recomend you to define in the object the method toString() so with a simple alert(MyArray); the full content of the array will be shown in the alert.
Here is an example:
//-------------------------------------------------------------------
// Defininf the Point object
function Point(CoordenadaX, CoordenadaY) {
// Sets the point coordinates depending on the parameters defined
switch (arguments.length) {
case 0:
this.x = null;
this.y = null;
break;
case 1:
this.x = CoordenadaX;
this.y = null;
break;
case 2:
this.x = CoordenadaX;
this.y = CoordenadaY;
break;
}
// This adds the toString Method to the point object so the
// point can be printed using alert();
this.toString = function() {
return " (" + this.x + "," + this.y + ") ";
};
}
Then if you have an array of points:
var MyArray = [];
MyArray.push ( new Point(5,6) );
MyArray.push ( new Point(7,9) );
You can print simply calling:
alert(MyArray);
Hope this helps!
Comments
You could write a function that will convert and format this array as string. Even better: use FireBug for debugging instead of alerts.
Comments
alert($("#form_id").serialize());
1 Comment
javascript which says "Unless a tag for a framework/library is also included, a pure JavaScript answer is expected". Even if that wasn't the case… the question is asking about an array, not an HTML form.
console.log-- it's great for introspection of JavaScript objects.console.debugwould actually work better.