Array 对象的 concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组.
var arr1 = ['a', 'b', 'c'];
var arr2 = ['d', 'e', 'f'];
console.log(arr1.concat(arr2));
输出 ["a", "b", "c", "d", "e", "f"]
在 vue 项目中使用出现 concat() 方法无效的情况
this.arr = [1, 2, 3]
console.log(this.arr) // [1, 2, 3]
this.arr.concat([4, 5])
console.log(this.arr) // [1, 2, 3]
上面的 concat() 方法无效,查MDN文档发现 concat() 返回一个新数组,如下写法就正常了
this.arr = [1, 2, 3]
console.log(this.arr) // [1, 2, 3]
this.arr = this.arr.concat([4, 5])
console.log(this.arr) // [1, 2, 3, 4, 5]