concat() 方法用于连接两个或多个数组。
我们创建了三个数组,然后使用 concat() 把它们连接起来: <script type="text/javascript">
var arr = new Array(3) //等同写法 var arr = ['George','John','Thomas'] 其实 就是语法糖 arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" var arr3 = new Array(2) arr3[0] = "William" arr3[1] = "Franklin" document.write(arr.concat(arr2,arr3)) </script> 输出: George,John,Thomas,James,Adrew,Martin,William,Franklin
用 ... 扩展运算代替
const arr1 = ['a', 'b']; const arr2 = ['c']; const arr3 = ['d', 'e']; // ES5 的合并数组 arr1.concat(arr2, arr3); // [ 'a', 'b', 'c', 'd', 'e' ] // ES6 的合并数组 [...arr1, ...arr2, ...arr3] // [ 'a', 'b', 'c', 'd', 'e' ]