Combining arrays in JS

后端 未结 2 1109
死守一世寂寞
死守一世寂寞 2020-12-11 21:11

If you have 2 arrays classical and pop:

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


        
2条回答
  •  一个人的身影
    2020-12-11 22:16

    + is first converting both arrays to string, and then adding the strings. For this you need to use 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"]
    

提交回复
热议问题