3

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
  • Some reserved words (like array or function) are part of JavaScript, including built-in object properties and HTML event listeners. You can't use reserved words as variable name. Commented Jan 22, 2021 at 15:12
  • @Wimanicesir just an FYI, array is not a reserved word. Commented Jan 22, 2021 at 15:17
  • Not explicitly no @evolutionxbox. But is it good practice to also avoid this. w3schools.com/js/js_reserved.asp Commented Jan 22, 2021 at 15:25

3 Answers 3

4

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'));

Sign up to request clarification or add additional context in comments.

Comments

3

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'));

Comments

0

Using .concat() and .trim():

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.