...
有序集合
let arr = [1, 2, 3, 4, 5];
console.log(...arr);// 1 2 3 4 5 6
let [a, b, ...c] = arr;
console.log(c);// [3, 4, 5]
function fn1 (...arg) {
console.log(arg);// [1, 2, 3, 4, 5, 6, 7]
}
fn1(1, 2, 3, 4, 5, 6, 7)
数组的深拷贝
let arr = [1, 2, 3, 4, 5]
1. for循环
let arr1 = [];
for (let i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
arr1.push(6);
console.log(arr);
console.log(arr1);
2. concat()
let arr2 = [].concat(arr);
arr2.push(7);
console.log(arr);
console.log(arr2);
3. ...
let arr3 = [...arr];// 第一种写法
let [...arr4] = arr;// 第二种写法
arr3.push(8);
arr4.push(9);
console.log(arr);
console.log(arr3);
console.log(arr4);
数组的拼接
let arr = [1, 2, 3];
let arr1 = [4, 5, 6];
console.log([...arr,...arr1]);
数组的操作方法
数组排序 .sort()
flat()
将对为数组展开成一维数组
通常用于数据处理
let arr = [1, [2, 3, [4, 5]], 6, 7]]
console.log(arr.flat(3)];
let arr1 = [[1, 2, [3, [4, 5, [6, 7, 8]]]]]
console.log(arr1.flat(4));
参数写几,就是展开几层
展开所有层,参数写 Infinity
let arr2 = [[1, 2, [3, [4, 5, [6, 7, 8, [9, 0]]]]]];
console.log(arr2.flat(Infinity));
forEach()
let arr = [1, 2, 3, 4, 5, 6, 7,8, 9];
arr.forEach((val, i, array) => {
console.log(val, i, array);
第一个参数:元素 value
第二个参数:索引值 index
第三个参数:原数组 array
参数名不固定,但是参数顺序固定
})