Combining arrays in JS

后端 未结 2 1104
死守一世寂寞
死守一世寂寞 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"]
    
    0 讨论(0)
  • 2020-12-11 22:18

    Use the Array class concat() method to combine them on a new variable:

    var all = classical.concat(pop);
    
    0 讨论(0)
提交回复
热议问题