JavaScript中数组拼接的几种方式
concat()方法拼接concat方法不会更改现有数组,而是返回一个新数组。let a = [1,2,3,4,5,6]let b = ['喵喵','旺财','大黄']//concatlet c = a.concat(b)console.log(c);//[1,2,3,4,5,6,'喵喵','旺财','大黄']循环拼接let a = [1,2,3,4,5,6]let b = ['喵喵','旺财',
·
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,'喵喵','旺财','大黄']
更多推荐
已为社区贡献1条内容
所有评论(0)