find
返回符合條件的第一個元素
如果沒有符合條件的元素則返回 undefined
注意:
- find 對空數組不執行
- find 不改變原數組
let arr = [1, 2, 3, 4, 5]
let find = arr.find((item) => {
return item % 2 === 0
})
find // 2
findIndex
返回符合條件的第一個元素位置
如果沒有符合條件的元素則返回 -1
注意:
- findIndex 對空數組不執行
- findIndex 不改變原數組
let arr = [1, 2, 3, 4, 5]
let findIndex = arr.findIndex((item) => {
return item % 2 === 0
})
findIndex // 1