1、indexOf():这个方法返回元素在数组中首次出现的位置。如果没有找到元素,则返回-1。
let array = [1, 2, 3, 4, 5];
console.log(array.indexOf(3));
console.log(array.indexOf(6));
2、includes():这个方法检查数组是否包含特定的元素。如果包含,返回true;如果不包含,返回false。
let array = [1, 2, 3, 4, 5];
console.log(array.includes(3));
console.log(array.includes(6));
3、find():这个方法返回数组中满足提供的测试函数的第一个元素的值。如果没有找到任何元素,则返回undefined。
let array = [1, 2, 3, 4, 5];
console.log(array.find(element => element > 2));
4、findIndex():这个方法返回数组中满足提供的测试函数的第一个元素的索引。如果没有找到任何元素,则返回-1。
let array = [1, 2, 3, 4, 5];
console.log(array.findIndex(element => element > 2));
5、some():这个方法检查是否有元素满足提供的测试函数。如果有元素满足测试函数,则返回true;否则返回false。
let array = [1, 2, 3, 4, 5];
console.log(array.some(element => element > 3));
6、every():这个方法检查所有元素是否都满足提供的测试函数。如果所有元素都满足测试函数,则返回true;否则返回false。
let array = [1, 2, 3, 4, 5];
console.log(array.every(element => element > 0));
7、forEach():这个方法遍历数组中的每个元素,并对每个元素执行提供的函数。
let array = [1, 2, 3, 4, 5];
array.forEach(element => console.log(element));