find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
find() 方法为数组中的每个元素都调用一次函数执行:
-
当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
-
如果没有符合条件的元素返回 undefined
find() 对于空数组,函数是不会执行的。
find() 并没有改变数组的原始值。
语法:
array.find(function(currentValue, index, arr),thisValue)
参数 | 描述 |
---|---|
function(currentValue, index,arr) | 必需。数组每个元素需要执行的函数。 函数参数:参数描述currentValue必需。当前元素index可选。当前元素的索引值arr可选。当前元素所属的数组对象 |
thisValue | 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值 |
返回值:返回符合测试条件的第一个数组元素值,如果没有符合条件的则返回 undefined。
示例:
let test = [1, 2, 3, 4, 5]; let a = test.find(item => item > 3); console.log(a); //4 let b = test.find(item => item == 0); console.log(b); //undefined
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)