JavaScript findIndex() 方法 JavaScript Array 对象 实例 获取数组中年龄大于等于 18 的第一个元素索引位置 [mycode3 type='js'] var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } function myFunction() { ..
Source Array (src) (源数组) 您的reducer函数的返回值分配给累计器,该返回值在数组的每个迭代中被记住,并最后成为最终的单个结果值。 arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue]) 注意:如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一...
Return Value: Returns the array element index if any of the elements in the array pass the test, otherwise it returns undefined JavaScript Version: ECMAScript 6More ExamplesExample Get the index of the first element in the array that has a value above a specific number: Minimum age: Try ...
代码语言:javascript 复制 // https://tc39.github.io/ecma262/#sec-array.prototype.findIndexif(!Array.prototype.findIndex){Object.defineProperty(Array.prototype,'findIndex',{value:function(predicate){// 1. Let O be ? ToObject(this value).if(this==null){thrownewTypeError('"this" is null or...
In this tutorial, you will learn about the JavaScript Array findIndex() method with the help of examples. The findIndex() method returns the index of the first array element that satisfies the provided test function or else returns -1.
Array Tutorials: Array Tutorial Array Const Basic Array Methods Array Search Methods Array Sort Methods Array Iteration Methods Browser Support findIndex()is an ECMAScript6 (ES6) feature. ES6 (JavaScript 2015) is supported in all modern browsers since June 2017: ...
function findIndexWithForLoop(array, condition) { for (let i = 0; i < array.length; i++) { if (condition(array[i])) { return i; } } return -1; } 使用Array.prototype.findIndex方法的替代方案:一些库或框架提供了针对性能优化的替代方案,例如Lodash库的findIndex方法。这些替代方案通常会使用...
array.findIndex(function(currentValue, index, arr),thisValue) currentValue : 必需。当前元素 index:可选。当前元素的索引值 arr: 可选。当前元素所属的数组对象 thisValue: 可选。 传递给函数的值一般用"this"值。 如果这个参数为空,"undefined" 会传递给 "this" 值 ...
array.findIndex(function(currentValue, index, arr),thisValue) currentValue : 必需。当前元素 index:可选。当前元素的索引值 arr: 可选。当前元素所属的数组对象 thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值 ...
❮PreviousJavaScript ArrayReferenceNext❯ Example 1 Find the last element with a value over 18: constages = [3,10,18,20]; ages.findLastIndex(checkAge); functioncheckAge(age) { returnage >18; } Try it Yourself » Description