- 需求:数组合并
- 传统方法
let arr1 = [{id: 0, name: '全部'}],
arr2 = [{id: 1, name: '新生'}, {id: 2, name: '老生'}];
for (let i = 0, len = arr2.length; i < len; i++) {
arr1.push(arr2[i]);
}
- 利用apply的特性
[].push(arr1, arr2);
let arr1 = [{id: 0, name: '全部'}],
arr2 = [{id: 1, name: '新生'}, {id: 2, name: '老生'}];
for (let i = 0, len = arr2.length; i < len; i++) {
arr1.push(arr2[i]);
}
[].push(arr1, arr2);