concat()方法拼接

concat方法不会更改现有数组,而是返回一个新数组。

let a = [1,2,3,4,5,6]
let b = ['喵喵','旺财','大黄']
//concat
let c = a.concat(b)
console.log(c);//[1,2,3,4,5,6,'喵喵','旺财','大黄']

循环拼接

let a = [1,2,3,4,5,6]
let b = ['喵喵','旺财','大黄']
for(let i=0;i<b.length;i++){
    a.push(b[i])
}
console.log(a);//[1,2,3,4,5,6,'喵喵','旺财','大黄']

push.apply()方法拼接

push.apply合并数组是把后一个数组的值依次push进前一个数组,使前一个数组发生改变

let a = [1,2,3,4,5,6]
let b = ['喵喵','旺财','大黄']
a.push.apply(a,b)
console.log(a);//[1,2,3,4,5,6,'喵喵','旺财','大黄']

es6结构赋值

let a = [1,2,3,4,5,6]
let b = ['喵喵','旺财','大黄']
a.push(...b)
console.log(a);//[1,2,3,4,5,6,'喵喵','旺财','大黄']
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐