How can I view array structure in JavaScript with alert()?

Javascript

Javascript Problem Overview


How can I view the structure of an array in JavaScript using alert()?

Javascript Solutions


Solution 1 - Javascript

A very basic approach is alert(arrayObj.join('\n')), which will display each array element in a row.

Solution 2 - Javascript

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 http://code.google.com/p/jquery-json/">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 http://getfirebug.com/">Firebug</a> 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.

Solution 3 - Javascript

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;
}

Solution 4 - Javascript

You can use alert(arrayObj.toSource());

Solution 5 - Javascript

I'd recommend using toString().

Ex. alert(array.toString()), or console.log(array.toString())

Solution 6 - Javascript

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.

Solution 7 - Javascript

For readability purposes you can use:

> alert(JSON.stringify(someArrayOrObj, '', 2));

More about JSON.stringify().

Example:

let user = {
  name: "John",
  age: 30,
  roles: {
    isAdmin: false,
    isEditor: true
  }
};

alert(JSON.stringify(user, "", 2));
/* Result:
{
  "name": "John",
  "age": 30,
  "roles": {
    "isAdmin": false,
    "isEditor": true
  }
} 
*/

Solution 8 - Javascript

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!

Solution 9 - Javascript

You could write a function that will convert and format this array as string. Even better: use FireBug for debugging instead of alerts.

Solution 10 - Javascript

Better use Firebug (chrome console etc) and use console.dir()

Solution 11 - Javascript

alert($("#form_id").serialize());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionppptttView Question on Stackoverflow
Solution 1 - JavascriptHumbertoView Answer on Stackoverflow
Solution 2 - JavascriptEli CourtwrightView Answer on Stackoverflow
Solution 3 - JavascriptFawad GhafoorView Answer on Stackoverflow
Solution 4 - JavascriptPiseth SokView Answer on Stackoverflow
Solution 5 - Javascriptdk123View Answer on Stackoverflow
Solution 6 - JavascriptJustin EthierView Answer on Stackoverflow
Solution 7 - JavascriptVictor S.View Answer on Stackoverflow
Solution 8 - JavascripttomasofenView Answer on Stackoverflow
Solution 9 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 10 - JavascriptyAnTarView Answer on Stackoverflow
Solution 11 - JavascriptMauricioView Answer on Stackoverflow