返回到:JavaScript对象:JavaScript Array 对象
find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
find() 方法为数组中的每个元素都调用一次函数执行:
注意: find() 对于空数组,函数是不会执行的。
注意: find() 并没有改变数组的原始值。
所有主流浏览器都支持 find() 方法。
注意: IE 11 及更早版本不支持 find() 方法。
array.find(function(currentValue, index, arr),thisValue)
参数 | 描述 |
---|---|
function(currentValue, index,arr) | 必需。数组每个元素需要执行的函数。 函数参数:参数描述currentValue必需。当前元素index可选。当前元素的索引值arr可选。当前元素所属的数组对象 |
thisValue | 可选。 传递给函数的值一般用 “this” 值。 如果这个参数为空, “undefined” 会传递给 “this” 值 |
返回值: | 返回符合测试条件的第一个数组元素值,如果没有符合条件的则返回 undefined。 |
---|---|
JavaScript 版本: | ECMAScript 6 |
DEMO1:返回符合大于输入框中数字的数组索引值。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web176教程(Web176.com)</title> </head> <body> <p>点击按钮返回符合大于输入框中指定数字的数组元素。</p> <p>最小年龄: <input type="number" id="ageToCheck" value="18"></p> <button >
DEMO2:获取数组中年龄大于 18 的第一个元素。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web176教程(Web176.com)</title> </head> <body> <p>点击按钮获取数组中年龄大于 18 的第一个元素。</p> <button >