針對陣列中的每個元素,執行callback,當全部
元素都符合條件,就回傳true
,否則回傳false
怎麼寫?
Array.prototype.every()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 === 18 } const boolean1 = number.every(callback) console.log(boolean1)
const boolean2 = number.every(function(element){ return element <= 10 }) console.log(boolean2)
const boolean3 = number.every(element => element < 200) console.log(boolean3)
|
參考
Array.prototype.every() - JavaScript | MDN