JS30_07 — Array Cardio Day 2

Huang Pei
2 min readOct 24, 2019

--

學習重點:some/ every/ find/ findIndex

some() 方法會透過給定函式、測試陣列中是否至少有一個元素,通過該函式所實作的測試。這方法回傳的是布林值

every() 方法會測試陣列中的所有元素是否都通過了由給定之函式所實作的測試。

find() 方法會回傳第一個滿足所提供之測試函式的元素。否則回傳 undefined

findIndex() 方法將依據提供的測試函式,尋找陣列中符合的元素,並返回其 index(索引)。如果沒有符合的對象,將返回 -1

算是帶入函式就能解的練習

  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)

--

--

Huang Pei
Huang Pei

Written by Huang Pei

記錄用倉庫,歡迎指正。菜鳥前端,最菜的那種(超能力少女です)。

No responses yet