針對陣列中的每個元素,執行callback,當至少一個
元素符合條件,就回傳true
,否則回傳false
怎麼寫?
Array.prototype.some()1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| const number = [18, 20, 65, 88, 131, 168]
function callback(element) { return element === 65 } const boolean1 = number.some(callback) console.log(boolean1)
const boolean2 = number.some(function(element){ return element < 20 }) console.log(boolean2)
const boolean3 = number.some(element => element > 200) console.log(boolean3)
|
參考
Array.prototype.some() - JavaScript | MDN