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访问到他们那一刻的值。那些被删除的元素或从来未被赋值的元素将不会被访问到。
示例
检测所有数组元素的大小
下例检测数组中的所有元素是否都大于 10。
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // trueUsing arrow functions
箭头函数为同一个测试提供了一个更短的语法。
[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // truePolyfill
在第 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()
© 2005–2017 Mozilla Developer Network and individual contributors.Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

