some()
方法會透過給定函式、測試陣列中是否至少有一個元素,通過該函式所實作的測試。這方法回傳的是布林值。
every()
方法會測試陣列中的所有元素是否都通過了由給定之函式所實作的測試。
find()
方法會回傳第一個滿足所提供之測試函式的元素值。否則回傳 undefined
。
findIndex()
方法將依據提供的測試函式,尋找陣列中符合的元素,並返回其 index(索引)。如果沒有符合的對象,將返回 -1 。
算是帶入函式就能解的練習
- Is at least one person 19 or older?
people.some(item => 2019 — item.year >= 19) //true
2. Is everyone 19 or older?
people.every(item => 2019 — item.year >= 19) //false
3. Find is like filter, but instead returns just the one you are looking for, find the comment with the ID of 823423
comments.find(item => item.id === 823423) // Object { text: "Super good", id: 823423 }
4. Find the comment with this ID, delete the comment with the ID of 823423
let indexId = comments.findIndex(item => item.id === 823423)
comments.splice(indexId, 1)