- filter() 筛选数组,根据筛选条件返回新数组, 不改变原数组
let arr = [1, 2, 3];
let newArr = arr.filter((item, index, array) => {
return item < 3
});
console.log(arr, newArr)
- find() 遍历数组, 满足条件就停止遍历并返回该元素
let arr = [1, 2, 3];
let newArr = arr.find((item, index, array) => {
return item < 3
});
console.log(newArr)
- findIndex() 遍历数组, 满足条件就停止遍历并返回该元素索引
let arr = [1, 2, 3];
let currentIndex= arr.findIndex((item, index, array) => {
return item < 3
});
console.log(currentIndex)
- includes() 判断数组里是否含有目标元素,返回布尔值
let arr = [1, 2, 3];
let isOk = arr.includes(3);
console.log(isOk)
- indexOf() 从数组中查找元素,存在返回索引,不存在返回-1
let arr = [1, 2, 3];
let index = arr.indexOf(3);
let index1 = arr.indexOf(2, 2);
console.log(index, index1)
- forEach() 遍历数组,遍历.length次数, 不能return
let arr = [1, 2, 3];
arr.forEach((item, index, array) => {
console.log(item, index, array)
})
- map() 映射数组, 不改变原数组
let arr = [1, 2, 3];
let newArr = arr.map((item, index, array) => {
return item * 3
});
console.log(arr, newArr)
- some() 判断数组里只要有一个元素符合条件,返回布尔值
let arr = [1, 2, 3];
let isOk = arr.some((item, index, array) => {
return item == 3
});
console.log(isOk)
- every() 判断数组里所有元素符合条件,返回布尔值
let arr = [1, 2, 3];
let isOk = arr.every((item, index, array) => {
return item < 3
});
console.log(isOk)