Join arrays in JS

If you have 2 classic and pop arrays:

classical=["Beethoven","Mozart","Tchaikovsky"]; pop=["Beatles","Corrs","Fleetwood Mac","Status Quo"]; 

Why does this, when you set all=classical+pop , give the character set in the array elements to a single character?

How do you fix this without redialing i.e. all=["Beethoven","Mozart","Tchaikovsky","Beatles"...]

Thank you very much in advance.

+4
source share
2 answers

Use the Array class method of class () to combine them into a new variable:

 var all = classical.concat(pop); 
+7
source

+ first converts both arrays to a string, and then adds strings. For this you need to use the concat method.

 > classical=["Beethoven","Mozart","Tchaikovsky"]; ["Beethoven", "Mozart", "Tchaikovsky"] > pop=["Beatles","Corrs","Fleetwood Mac","Status Quo"]; ["Beatles", "Corrs", "Fleetwood Mac", "Status Quo"] > all = classical.concat(pop) ["Beethoven", "Mozart", "Tchaikovsky", "Beatles", "Corrs", "Fleetwood Mac", "Status Quo"] 
+4
source

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


All Articles