JavaScript参考手册
排列 | Array
array.every
every()
方法测试数组的所有元素是否都通过了指定函数的测试。
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
语法
arr.every(callback[, thisArg])
参数
callback
函数为每个元素测试,取三个参数:currentValue
(必需)在数组中处理的当前元素。index
(可选)数组中正在处理的元素的索引。array
(可选)数组every
被调用。thisArg
可选。执行callback
时使用this
的值。
返回值
如果callback
函数为每个数组元素返回一个正确值,则为true
; 否则,为false
。
描述
every
方法为数组中的每个元素执行一次 callback
函数,直到它找到一个使 callback
返回 false(表示可转换为布尔值 false 的值)的元素。如果发现了一个这样的元素,every
方法将会立即返回 false
。否则,callback
为每一个元素返回true
,every
就会返回 true
。callback
只会为那些已经被赋值的索引调用。不会为那些被删除或从来没被赋值的索引调用。
callback
被调用时传入三个参数:元素值,元素的索引,原数组。
如果thisArg
提供了一个参数every
,它将被用作callback
的this
值。否则,该值undefined
将被用作其this
值。this
最终可观察到的值callback
是根据用于确定this
函数所看到的通常规则来确定的。
every
不会改变原数组。
every
遍历的元素范围在第一次调用callback
之前就已确定了。在调用every
之后添加到数组中的元素不会被callback
访问到。如果数组中存在的元素被更改,则他们传入callback
的值是every
访问到他们那一刻的值。那些被删除的元素或从来未被赋值的元素将不会被访问到。
every
就像数学中的“所有”量词一样。特别是对于一个空数组,它返回true。(空集的所有元素都满足任何给定的条件是真实的。)
示例
检测所有数组元素的大小
下例检测数组中的所有元素是否都大于 10。
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
Using arrow functions
箭头函数为同一个测试提供了一个更短的语法。
[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true
Polyfill
在第 5 版时,every
被添加进 ECMA-262 标准;因此在某些实现环境中不被支持。你可以把下面的代码放到脚本的开头来解决此问题,该代码允许在那些没有原生支持every
的实现环境中使用它。该算法是 ECMA-262 第5版中指定的算法,假定 Object
和 TypeError
拥有它们的初始值,且 callbackfn.call
等价于Function.prototype.call
。
if (!Array.prototype.every) {
Array.prototype.every = function(callbackfn, thisArg) {
'use strict';
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
// 1. Let O be the result of calling ToObject passing the this
// value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method
// of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (typeof callbackfn !== 'function') {
throw new TypeError();
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0.
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal
// method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method
// of O with argument Pk.
kValue = O[k];
// ii. Let testResult be the result of calling the Call internal method
// of callbackfn with T as the this value and argument list
// containing kValue, k, and O.
var testResult = callbackfn.call(T, kValue, k, O);
// iii. If ToBoolean(testResult) is false, return false.
if (!testResult) {
return false;
}
}
k++;
}
return true;
};
}
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 5.1 (ECMA-262)The definition of 'Array.prototype.every' in that specification. |
Standard |
Initial definition. Implemented in JavaScript 1.6. |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.prototype.every' in that specification. |
Standard |
|
ECMAScript Latest Draft (ECMA-262)The definition of 'Array.prototype.every' in that specification. |
Living Standard |
|
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
1.5 |
9 |
(Yes) |
(Yes) |
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
1 |
(Yes) |
(Yes) |
(Yes) |
也请参阅
Array.prototype.forEach()
Array.prototype.some()
TypedArray.prototype.every()
Edit this page on MDN
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
排列 | Array相关
JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络