JavaScript Array.findIndex()用法及代码示例Array.findIndex()JavaScript 中的方法用于查找数组中满足所提供的测试函数的第一个元素的索引。它返回测试函数返回 true 的第一个元素的索引。如果没有找到这样的元素,则返回-1。用法:array.findIndex(function(currentValue, index, arr), thisValue);...
Finding a value in an array is useful for effective data analysis. Learn how to discover what a JavaScript Array contains using indexOf, includes, for loop, some methods and more.
Create a tag to add JavaScript file. First, create an array containing numbers Create a function that returns a value that helps to find the element. Then create a variable contains findIndex(). Using document.write() to only find the index value. Conclusion ...
callback- Function to execute on each element of the array. It takes in: element- The current element of array. thisArg(optional) - Object to use asthisinsidecallback. findIndex() Return Value Returns theindexof thefirst elementin the array that satisfies the given function. Returns-1if no...
JavaScript 中 findIndex 与indexOf 的主要区别在于 findIndex 接受回调作为参数,而 indexOf 接受值作为参数。 这意味着 indexOf 只会在数组中查找值,而 findIndex 将让你决定如何查找索引。 下面是Array.prototype.findIndex方法与Array.prototype.indexOf方法之间差异的直观示例: ...
Array findIndex() function in JavaScript - The findIndex() function in JavaScript returns the index of the first element value that satisfy a given condition in an array.Following is the code for the array find() function −Example Live Demo
In JavaScript, we can use thearray.findIndex()method to get the first element from the array that matches a particular condition. We need to use a function and the current value as parameters in thearray.findIndex()method. If an element matches the condition, the function will return that...
array.find(function(currentValue, index, arr),thisValue) currentValue : 必需。当前元素 index:可选。当前元素的索引值 arr: 可选。当前元素所属的数组对象 thisValue: 可选。 传递给函数的值一般用"this"值。 如果这个参数为空,"undefined" 会传递给 "this" 值 ...
Source Array (src) (源数组) 您的reducer函数的返回值分配给累计器,该返回值在数组的每个迭代中被记住,并最后成为最终的单个结果值。 arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue]) 注意:如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一...
findIndex方法:定制版的indexOf,找到返回索引,找不到返回-1 let index3 = arr.findIndex(function (currentValue, currentIndex, currentArray) { if (currentValue === 6){ return true; } }); console.log(index3);//2 1. 2. 3. 4. 5. ...