- Includes方法:判断数组是否包含某个值
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // true
console.log(arr.includes(6)); // false
- Find方法:返回数组中符合条件的第一个元素
const arr = [1, 2, 3, 4, 5];
const found = arr.find(element => element > 2);
console.log(found); // 3
- FindIndex方法:返回数组中符合条件的第一个元素的索引
const arr = [1, 2, 3, 4, 5];
const foundIndex = arr.findIndex(element => element > 2);
console.log(foundIndex); // 2
- Filter方法:返回数组中符合条件的所有元素
const arr = [1, 2, 3, 4, 5];
const filtered = arr.filter(element => element > 2);
console.log(filtered); // [3, 4, 5]
- Map方法:将数组中的每个元素进行处理,并返回新的数组
const arr = [1, 2, 3, 4, 5];
const doubled = arr.map(element => element * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
- Reduce方法:将数组中的元素累加或累乘
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(sum); // 15
- Spread语法:用于数组拷贝、合并
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const mergedArr = [...arr1, ...arr2];
console.log(mergedArr); // [1, 2, 3, 4, 5, 6]
- Set数据结构:去重数组
const arr = [1, 2, 2, 3, 3, 4];
const uniqueArr = [...new Set(arr)];
console.log(uniqueArr); // [1, 2, 3, 4]
- Array.from方法:将类数组对象或可迭代对象转化为数组
const nodeList = document.querySelectorAll('div');
const arr = Array.from(nodeList);
console.log(arr); // array of div elements
- Array.of方法:创建一个指定元素的数组
const arr = Array.of(1, 2, 3, 4, 5);
console.log(arr); // [1, 2, 3, 4, 5]
- Array.fill方法:用指定值填充数组
const arr = new Array(5);
arr.fill(0);
console.log(arr); // [0, 0, 0, 0, 0]
- Array.some方法:判断数组中是否存在满足条件的元素
const arr = [1, 2, 3, 4, 5];
const hasEven = arr.some(element => element % 2 === 0);
console.log(hasEven); // true
- Array.every方法:判断数组中所有元素是否都满足条件
const arr = [1, 2, 3, 4, 5];
const allEven = arr.every(element => element % 2 === 0);
console.log(allEven); // false
- Array.flat方法:将嵌套数组扁平化
const arr = [1, 2, [3, 4], [5, [6, 7]]];
const flatArr = arr.flat(2);
console.log(flatArr); // [1, 2, 3, 4, 5, 6, 7]
- Array.flatMap方法:先对数组中的每个元素执行map方法,再将结果扁平化
const arr = [1, 2, 3];
const mappedArr = arr.flatMap(element => [element, element * 2]);
console.log(mappedArr); // [1, 2, 2, 4, 3, 6]
- Object.entries方法:返回对象可遍历的键值对数组
const obj = { a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // [['a', 1], ['b', 2], ['c', 3]]
- Object.values方法:返回对象可遍历的值数组
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // [1, 2, 3]
- Object.keys方法:返回对象可遍历的键数组
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // ['a', 'b', 'c']
- Object.assign方法:合并对象
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const mergedObj = Object.assign({}, obj1, obj2);
console.log(mergedObj); // { a: 1, b: 2 }
- 对象解构赋值:从对象中提取属性,赋值给变量
const obj = { a: 1, b: 2 };
const { a, b } = obj;
console.log(a, b); // 1 2
原文链接:https://juejin.cn/post/7341592148306001929 作者:前端fighter