JavaScript > 标准内置对象 > 数组 > Array.prototype.concat( )
concat()
方法用于 合并
两个或多个数组。此方法会返回一个新的数组,不会改变原有数组
语法:
- 二合一:Array.concat( value0 )
- 三合一:Array.concat( value0 , value1 )
- 多合一:Array.concat( value0 , value1 , … ,valueN )
参数:
valueN:要连接成新数组的数组或值
案例:
// 定义两个数组
const arr1 = ['a','b','c']
const arr2 = ['d','e','f']
// 使用数组的 concat 方法,将两个数组合并为一个数组
const arrResult = arr1.concat(arr2)
console.log(arrResult); // (6) ['a', 'b', 'c', 'd', 'e', 'f']
// 定义三个数组
const num1 = [1,2,3]
const num2 = [4,5,6]
const num3 = [7,8,9]
// 使用数组的 concat 方法,将三个数组合并为一个数组
const numberArr = num1.concat(num2,num3)
console.log(numberArr); // (9) [1, 2, 3, 4, 5, 6, 7, 8, 9]