find
语法:arr.find(callback(currentEle[,index[,array]])[,thisArg])
callback:数组中每一项元素执行的回调函数,接收三个参数
currentEle:(必选)当前正在处理的元素
index:(可选)正在处理的元素的下标
array:(可选)数组本身
thisArg:(可选)执行回调函数时指定的this值
返回值:返回数组中满足回调函数找到的第一个值,否则返回undefined
const arr = ['red', 'green', 'blue', 'gray', 'pink', 'yellow']
const result = arr.find(function(item) {
return item.length > 4
})
result // green
findIndex
语法:arr.findIndex(callback(element[, index[, arr]])[, thisArg])
callback:数组中每个元素执行的回调函数,可以接受三个参数
element:(必选)执行回调函数的当前元素
index:(可选)当前元素的下标
arr:(可选)数组本身
thisArg:(可选)执行回调函数时指定的this 的值
返回值:返回数组中第一个满足回调函数的元素的下标,如果没有找到对应的元素则返回-1
const arr = ['red', 'green', 'blue', 'gray', 'pink', 'yellow']
const result = arr.findIndex(function(item) {
return item.length > 4
})
result // 1