admin管理员组

文章数量:1390941

Is writing an for-loop in an alert-statement possible?

Closer look to problem: If I want to print the content of an array in one alert-statement. I am wondering if this is even possible.

an array :

myArray["a1", "a2", "a3", "a4"]

Printing something like following with/in just one alert-statement:

Here your great content of myArray: 
a1 
a2 
a3 
a4

I am thinking about something like that:

alert("
Here your great content of myArray:\n" +

for (var i = 0; i < myArray.length; i++) {
     myArray[i] + "\n";
}

);

Whereas of course it does not work .., but I kind of got stuck. Hope for constructive help, even if it might seem to be a silly question at first look.

Thanks in advance

Is writing an for-loop in an alert-statement possible?

Closer look to problem: If I want to print the content of an array in one alert-statement. I am wondering if this is even possible.

an array :

myArray["a1", "a2", "a3", "a4"]

Printing something like following with/in just one alert-statement:

Here your great content of myArray: 
a1 
a2 
a3 
a4

I am thinking about something like that:

alert("
Here your great content of myArray:\n" +

for (var i = 0; i < myArray.length; i++) {
     myArray[i] + "\n";
}

);

Whereas of course it does not work .., but I kind of got stuck. Hope for constructive help, even if it might seem to be a silly question at first look.

Thanks in advance

Share Improve this question asked Dec 5, 2014 at 15:16 user2835294user2835294 31 silver badge2 bronze badges 2
  • IMHO no you can't :) – Sundar Rajan Commented Dec 5, 2014 at 15:18
  • You could write something like alert("Here your great content of myArray:\n" + myArray.join("\n")) – Jan Sommer Commented Dec 5, 2014 at 15:19
Add a ment  | 

8 Answers 8

Reset to default 5

I want to print the content of an array in one alert-statement.

No need for a loop. Just use .join() to create a string separated by a newline character (or whatever character(s) you want).

alert("Here your great content of myArray:\n" + myArray.join("\n"));

And FYI, in ECMAScript 6, you'll be able to do this:

alert("Here your great content of myArray:\n" + [for (s of myArray) s + "\n"].join(""));

This can be handy for more plex situations.

In alert you can pass a string. You passed also a for statement. This is not correct. In order you get that you want, I suggest you build a function that builds this string and then call it in the alert.

Something like this:

function getMessage(){
    var text="Here your great content of myArray:\n";    
    for (var i = 0; i < myArray.length; i++) {
     text += myArray[i] + "\n";
    }
}

alert(getMessage());

The question is: can you convert a loop to a string? No you can't. This is a syntax error.

No, but you can do:

var text;    
for (var i = 0; i < myArray.length; i++) {
     text += myArray[i] + "\n";
}
alert("Here your great content of myArray:\n" + text);

sort of:

alert("Here your great content of myArray:\n" + myArray.map(function(i){ 
          return i + "\n"
}).join(''))

You can't pass the for statement to the alert, but you can create an string with the array data and pass it to the alert as follow:

array = ['some', 'text', 'to', 'print']

alert("message: " + array.map(function(s){return s;}))

join is good, but for dict objects you can't use it

array = [{text:'some'}, {text:'text'}, {text:'to'}, {text:'print'}]

alert("message: " + array.map(function(s){return s['text'];}))

hope it helps

There is a syntax error in you cannot execute javascript in side the alert function

var myArray=["a1", "a2", "a3", "a4"];

var alertContent= "Here your great content of myArray:\n" ;

for(var i=0;i<myArray.length;i++){ 
    alertContent+=myArray[i]+'\n';
}
alert(alertContent);

you can use the above code to get the out put that you want

The obvious answer would be to use .join() as suggested by Jan but I am going to assume that you want to do something within the loop to decide which of the array elements to include e.g. only if it starts with a (which in this case would be all of them anyway but hey ho). In this case the way to do it would be to create the alert text as a variable and then alert the variable. Something like...

myArray["a1", "b2", "b3", "a4"]
msg = "Here is your great content of myArray:\n";
for (var i = 0; i < myArray.length; i++) {
    if(myArray[i].substring(0,1) == "a") {
        msg+=myArray[i] + "\n";
        }
    }
alert(msg);

which will result in

Here is your great content of myArray:
a1
a4

本文标签: javascriptis an alert in a forloop possibleStack Overflow