The find() method does not change the original array.Array Find Methods: MethodFinds indexOf() The index of the first element with a specified value lastIndexOf() The index of the last element with a specified
在JavaScript中,array.find()是一个数组方法,用于在数组中查找满足指定条件的第一个元素,并返回该元素。如果找到匹配的元素,则返回该元素;否则返回undefined。 array.find()方法接受一个回调函数作为参数,该回调函数可以接受三个参数:当前元素、当前索引和原始数组。回调函数应返回一个布尔值,用于判断当前元素是否满足...
The find() method returns the value of the first element in an array that pass a test (provided as a function).The find() method executes the function once for each element present in the array:If it finds an array element where the function returns a true value, find() returns the ...
ThefindLastIndex()method does not change the original array. Array Find Methods: MethodFinds indexOf()The index of the first element with a specified value lastIndexOf()The index of the last element with a specified value find()The value of the first element that passes a test ...
The findIndex() method returns the index of the first element in an array that pass a test (provided as a function).The findIndex() method executes the function once for each element present in the array:If it finds an array element where the function returns a true value, findIndex()...
JavaScript 中文开发手册 array.find (Array) - JavaScript 中文开发手册 find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined。 1 2 3 4 5 functionisBigEnough(element) { returnelement >= 15; } [12, 5, 8, 130, 44].find(isBigEnough);// 130 ...
简单地说,find不期望返回承诺,因为它不适用于异步事物。它循环遍历数组,直到其中一个元素导致返回真值。一个对象,包括一个 promise 对象,是真实的,所以查找在第一个元素上停止。 如果您想要一个与 find 等效的异步方法,则需要自己编写。您需要考虑的一个因素是您是想并行运行,还是想按顺序运行,在移动到下一个索引...
如果您需要兼容不支持Object.defineProperty的JavaScript引擎,那么最好不要对Array.prototype方法进行 polyfill ,因为您无法使其成为不可枚举的。 规范 Specification Status Comment ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.prototype.find' in that specification. ...
代码语言:javascript 复制 arr.find(callback[,thisArg]) 参数 callback在数组每一项上执行的函数,接收 3 个参数: element当前遍历到的元素。 index当前遍历到的索引。 array数组本身。 thisArg可选,指定callback 的 this 参数。 返回值 当某个元素通过 callback 的测试时,返回数组中的一个值,否则返回undefined。
Example 1: Using findIndex() method // function that returns even numberfunctionisEven(element){returnelement %2==0; }// defining an array of integersletnumbers = [1,45,8,98,7]; // returns the index of the first even number in the arrayletfirstEven = numbers.findIndex(isEven); ...