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 elements of an array using a warning window?

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

Change . I want to display this array as a php function print_r.

 output needed like: array["key" => "value", "key" => "value", ...];
+4
source share
6 answers

You can also use the JavaScript function toString().

alert(arr.toString());
+3
source

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

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

for printing individually:

$.each(arr, function( index, value ) {
  alert( value );
})
+2
source
var arr = [1,2,3,4,5,6,7,8,9,10];
alert(arr);
for(var i = 0 ; i < arr.length; i++){
alert("key "+ i + " and " + "Value is "+arr[i]);
}

FIDDLE

,

+1

, console.log() , .

:

console.log(arr);

(F12 ) . .

+1
source

var a = {
  "1": 15,
  "2": 16,
  "3": 17,
}

console.log(a);
Run codeHide result
0
source
    var arr = [1,2,3,4,5,6,7,8,9,10];
    var arrstr="arr[";
    for(var i=0;i<arr.length;i++){
        arrstr+="\""+i+"\" : \""+arr[i]+"\"";   //you can change ":" for "=>", if you like
        if(i!=arr.length-1){//if not the last one ,add "," 
            arrstr+=",";    
        }else{
            arrstr+="]";
        }
    }

    alert(arrstr);
0
source

Source: https://habr.com/ru/post/1606539/


All Articles