How to list array inside a javascript alert

Viewed 165

I have an array of strings, I want to display the content with some html tags in the alert level. I tried this:

array = ["toto", "titi"]
alert("*" + array.join('\n'))

but the problem here, I got only the first line with *. how can I resolvr this probleme to show all the lements with * in the begining ?

3 Answers

Array.map()

You should use map method to create a new array populated with each element having * prefix.

const array = ["foo", "bar"];

alert(array.map(value => `*${value}`).join('\n'));

Before joining the array you can map it to add * in front of each entry with arrayOfStrings.map(i => '*' + i)

arrayOfStrings = ["toto", "titi"];

alert(arrayOfStrings.map(i => '*' + i).join('\n'));

Using .concat() and .trim():

var arrStr =  ["toto","titi"];

alert([''].concat(arrStr).join('\n*').trim());

Related