how to do a “flat push” in javascript?

前端 未结 4 840
不知归路
不知归路 2020-12-15 04:31

I want to push all individual elements of a source array onto a target array,

target.push(source);

puts just source\'s reference on the tar

4条回答
  •  天命终不由人
    2020-12-15 05:13

    You could use the concat method:

    var num1 = [1, 2, 3];  
    var num2 = [4, 5, 6];  
    var num3 = [7, 8, 9];  
    
    // creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged  
    var nums = num1.concat(num2, num3);
    

提交回复
热议问题