Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print/display an array in jquery

I have an array

var arr = [1,2,3,4,5,6,7,8,9,10];

How to display all items of the array using an alert box?

I have tried : alert(arr); and it shows nothing.

Edit: I want display this array like php print_r function.

 output needed like: array["key" => "value", "key" => "value", ...];
like image 983
Deepak Avatar asked Sep 09 '15 06:09

Deepak


3 Answers

You could also use the JavaScript function toString().

alert(arr.toString());
like image 132
Holonaut Avatar answered Sep 28 '22 18:09

Holonaut


To show them in csv, you can use .join(",") along with array object:

alert(arr.join(", "));

for printing individually:

$.each(arr, function( index, value ) {
  alert( value );
})
like image 39
Milind Anantwar Avatar answered Sep 28 '22 20:09

Milind Anantwar


As I'm wondering why console.log() hasn't been provided as an answer, here it is.

Do:

console.log(arr);

And open the developper toolbar (F12 on most browsers) and go to the console tab. You should be able to see and expand your array.

like image 21
D4V1D Avatar answered Sep 28 '22 19:09

D4V1D