JavaScript中两个数组的拼接方法合集
方法一:使用for循环
|
1
2
3
4
5
6
7
8
|
<strong>var arr = ['tom', 'jerry'];var arr2 = [1, 2];for(var i=0; i<arr2.length; i++){ arr.push(arr2[i])}console.log(arr);// ['tom', 'jerry', 1, 2]</strong> |
|
1
2
3
4
5
6
|
<strong>var arr = ['tom', 'jerry'];var arr2 = [1, 2];var newArr = arr.concat(arr2);console.log(newArr);// ["tom", "jerry", 1, 2]</strong> |
|
1
2
3
4
5
6
|
<strong>var arr = ['tom', 'jerry'];var arr2 = [1, 2];arr.push.apply(arr, arr2);console.log(arr)// ["tom", "jerry", 1, 2]</strong> |
|
1
2
3
4
5
6
|
<strong>var arr = ['tom', 'jerry'];var arr2 = [1, 2];arr.push(...arr2);console.log(arr)// ["tom", "jerry", 1, 2]</strong> |

浙公网安备 33010602011771号