1. 循环
a. 循环项
const arr = [1, 2, 3];
for (let item of arr) {
console.log(item);
}
// 输出:1 2 3
b. 循环键
const arr = [1, 2, 3];
for (let key in arr) {
console.log(key);
}
// 输出:0 1 2
c. 循环键值对
const arr = [1, 2, 3];
arr.forEach((item, index) => {
console.log(item);
console.log(index);
});
// 输出:1 0 2 1 3 2
3. 修改项
let arr = [1, 2, 3];
arr = arr.map(item => item + 1);
console.log(arr);
// 输出:[2, 3, 4]
5. 排序
a. 条件排序
注:sort中的方法第一个参数是从第二项开始的
返回值小于0:current在previous之前
返回值大于0:current在previous之后
let arr = [1, 2, 3];
arr = arr.sort((current, previous) => previous - current);
console.log(arr);
// 输出:[3, 2, 1]
arr = arr.sort((current, previous) => current - previous);
console.log(arr);
// 输出:[1, 2, 3]
b. 倒叙
let arr = [1, 2, 3];
arr = arr.reverse();
console.log(arr);
// 输出:[3, 2, 1]
6. 条件
a. 过滤
let arr = [1, 2, 3];
arr = arr.filter(item => item >= 2);
console.log(arr);
// 输出:[2, 3]
b. 获取指定项
const arr = [1, 2, 3];
const a = arr.find(item => item === 2);
console.log(a);
// 输出:2
c. 包含满足指定条件项
const arr = [1, 2, 3];
const b = arr.some(item => item === 2);
console.log(b);
// 输出:true
const b2 = arr.some(item => item === 0);
console.log(b2);
// 输出:false
d. 全部满足指定条件
const arr = [1, 2, 3];
const b = arr.every(item => item === 2);
console.log(b);
// 输出:false
const b2 = arr.every(item => item > 0);
console.log(b2);
// 输出:true
7. 拼接
a. 拼接转字符串
const arr = [1, 2, 3];
const s = arr.join(",");
console.log(s);
// 输出:1,2,3
b. 拼接数组
const arr = [1, 2, 3];
const arr2 = [4, 5, 6];
const a = arr.concat(arr2);
console.log(a);
// 输出:[1, 2, 3, 4, 5, 6]
c. 截取指定片段
arr.slice(起始位置(从0开始),结束位置(不包含));
const arr = [1, 2, 3];
const a = arr.slice(1, 2);
console.log(a);
// 输出:[2]
d. 截取掉并替换指定片段(或删除)
arr.splice(起始位置(从0开始), 替换数量, 替换数据(没有的话相当于删除))
const arr = [1, 2, 3];
const a = arr.splice(0, 1);
console.log(arr);
console.log(a);
// 输出:[2, 3] [1]
e. 添加项
const arr = [1, 2, 3];
arr.push(4);
console.log(arr);
// 输出:[1, 2, 3, 4]
f. 头部添加项
头部添加项,并返回新数组长度
const arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
// 输出:[0, 1, 2, 3]
g. 删除项
const arr = [1, 2, 3];
const a = arr.pop();
console.log(arr);
console.log(a);
// 输出:[1, 2] 3
h. 删除头部项
删除头部项,并返回删除的项
const arr = [1, 2, 3];
arr.shift();
console.log(arr);
// 输出:[2, 3]
8. 去重
一个高阶的数组方法:reduce
const arr = [1, 2, 4, 3, 4];
const arr2 = arr.reduce((acc, cur) => {
if (!acc.includes(cur)) {
acc.push(cur);
}
return acc;
}, []);
console.log(arr2);
// 输出:[1, 2, 4, 3]
说明
// total:初始值, 或者计算结束后的返回值
// currentValue:当前元素
// currentIndex:当前元素的索引
// arr:当前元素所属的数组对象
// initialValue: 初始值
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
9. 集合论
a. 取交集
const arr1 = [1, 2, 3];
const arr2 = [2, 4, 5];
const arr = arr1.filter(item => arr2.includes(item));
console.log(arr);
// 输出:[2]
b. 取并集
const arr1 = [1, 2, 3];
const arr2 = [2, 4, 5];
const arr = [...arr1, ...arr2].reduce((acc, curr) => {
if (!acc.includes(curr)) {
acc.push(curr);
}
return acc;
}, []);
console.log(arr);
// 输出:[1, 2, 3, 4, 5]
c. 取差集
const arr1 = [1, 2, 3];
const arr2 = [2, 4, 5];
const arrf = arr1.filter(item => arr2.includes(item));
const arr = [...arr1, ...arr2].reduce((acc, curr) => {
if (!acc.includes(curr) && !arrf.includes(curr)) {
acc.push(curr);
}
return acc;
}, []);
console.log(arr);
// 输出:[1, 3, 4, 5]