確定數組的所有成員是否滿足指定的測試。
## 語法
~~~
array1.every(callbackfn[, thisArg])
~~~
## 參數
|參數|定義|
|--|--|
|array1|必需。一個數組對象。|
|callbackfn|必需。一個接受最多三個參數的函數。 every 方法會為 array1 中的每個元素調用 callbackfn 函數,直到 callbackfn 返回 false,或直到到達數組的結尾。|
|thisArg|可選。可在 callbackfn 函數中為其引用 this 關鍵字的對象。如果省略 thisArg,則 undefined 將用作 this 值。|
## 返回值
如果 callbackfn 函數為所有數組元素返回 true,則為 true;否則為 false。如果數組沒有元素,則 every 方法將返回 true。
## 異常
如果 callbackfn 參數不是函數對象,則將引發 TypeError 異常。
~~~
Exception Condition
~~~
## 備注
every 方法會按升序順序對每個數組元素調用一次 callbackfn 函數,直到 callbackfn 函數返回 false。如果找到導致 callbackfn 返回 false 的元素,則 every 方法會立即返回 false。否則,every 方法返回 true。
不為數組中缺少的元素調用該回調函數。
除了數組對象之外,every 方法可由具有 length 屬性且具有已按數字編制索引的屬性名的任何對象使用。
System_CAPS_note注意
可以使用 some 方法 (Array) (JavaScript)檢查回調函數是否對數組的任何元素均返回 true。
### 回調函數語法
回調函數的語法如下所示:
~~~
function callbackfn(value, index, array1)
~~~
可使用最多三個參數來聲明回調函數。
下表列出了回調函數參數。
|回調參數|定義|
|--|--|
|Value|數組元素的值。|
|index|數組元素的數字索引。|
|array1|包含該元素的數組對象。|
### 修改數組對象
數組對象可由回調函數修改。
下表描述了在 every 方法啟動后修改數組對象所獲得的結果。
|every 方法啟動后的條件|元素是否傳遞給回調函數|
|--|--|
|在數組的原始長度之外添加元素。|否。|
|添加元素以填充數組中缺少的元素。|是,如果該索引尚未傳遞給回調函數。|
|元素被更改。|是,如果該元素尚未傳遞給回調函數。|
|從數組中刪除元素。|否,除非該元素已傳遞給回調函數。|
下面的示例闡釋了 every 方法的用法。
~~~
// Define the callback function.
function CheckIfEven(value, index, ar) {
document.write(value + " ");
if (value % 2 == 0)
return true;
else
return false;
}
// Create an array.
var numbers = [2, 4, 5, 6, 8];
// Check whether the callback function returns true for all of the
// array values.
if (numbers.every(CheckIfEven))
document.write("All are even.");
else
document.write("Some are not even.");
// Output:
// 2 4 5 Some are not even.
下面的示例闡釋 thisArg 參數的用法,該參數指定對其引用 this 關鍵字的對象。
JavaScript
// Create a function that returns true if the value is
// numeric and within range.
var checkNumericRange = function(value) {
if (typeof value !== 'number')
return false;
else
return value >= this.minimum && value <= this.maximum;
}
// Create an array of numbers.
var numbers = [10, 15, 19];
// Check whether the callback function returns true for
// all of the array values.
// The obj argument enables use of the this value
// within the callback function.
var obj = { minimum: 10, maximum: 20 }
if (numbers.every(checkNumericRange, obj))
document.write ("All are within range.");
else
document.write ("Some are not within range.");
// Output:
// All are within range.
~~~
- ActiveXObject對象
- Array對象
- constructor屬性
- length屬性
- prototype屬性
- Array.from函數
- Array.isArray函數
- Array.of函數
- concat方法
- entries方法
- every方法
- fill方法
- filter方法
- findIndex方法
- forEach方法
- indexOf方法
- join方法
- keys方法
- lastIndexOf方法
- map方法
- pop方法
- push方法
- reduce方法
- reduceRight方法
- reverse方法
- shift方法
- slice方法
- some方法
- sort方法
- splice方法
- toString方法
- unshift方法
- valueOf方法
- values方法
- ArrayBuffer對象
- byteLength屬性
- ArrayBuffer.isView函數
- slice方法
- arguments對象
- 0...n 屬性(參數)
- callee 屬性(參數)
- length 屬性 (arguments)
- Boolean對象
- constructor 屬性(布爾值)
- prototype 屬性(布爾值)
- toString 方法 (Boolean)
- valueOf 方法 (Boolean)
- DataView對象
- buffer屬性 (DataView)
- byteLength屬性(DataView)
- byteOffset屬性(DataView)
- getInt8方法(DataView)
- getUint8方法(DataView)
- getInt16方法(DataView)
- getUint16方法(DataView)
- getInt32方法(DataView)
- getUint32方法(DataView)
- getFloat32方法(DataView)
- getFloat64方法(DataView)
- setInt8方法(DataView)
- setUint8方法(DataView)
- setInt16方法(DataView)
- setUint16方法(DataView)
- setInt32方法(DataView)
- setUint32方法(DataView)
- setFloat32方法(DataView)
- setFloat64方法(DataView)
- Date對象
- Debug對象
- Enumerator對象
- Error對象
- Float32Array對象
- Float64Array對象
- Function對象
- Global對象
- Int8Array對象
- Int16Array對象
- Int32Array對象
- Intl.Collator對象
- Intl.DateTimeFormat對象
- Intl.NumberFormat對象
- JSON對象
- Map對象
- Math對象
- Number對象
- Object對象
- Promise對象
- 代理對象
- Reflect對象
- RegExp對象
- 正則表達式對象
- Set對象
- String對象
- 符號對象
- Uint8Array對象
- Uint8ClampedArray對象
- Uint16Array對象
- Uint32Array對象
- VBArray對象
- WeakMap對象
- WeakSet對象
- WinRTError對象