TypedArray 实例的 every() 方法测试类型化数组的所有元素是否都能够通过由提供的函数实现的测试。其返回一个布尔值。这个方法的算法与 Array.prototype.every() 相同。
JavaScript Demo: TypedArray.every()
function isNegative(element, index, array) {
return element < 0;
}
const int8 = new Int8Array([-10, -20, -30, -40, -50]);
console.log(int8.every(isNegative));
// Expected output: true
语法
every(callbackFn)
every(callbackFn, thisArg)
参数
callbackthisArg- : 该值会作为执行
callbackFn时的this值。参见迭代方法。
- : 该值会作为执行
返回值
返回 true,除非 callbackFn 对类型化数组中的某个元素返回了falsy值(在这种情况下,立即返回 false)。
描述
参见 Array.prototype.every() 以了解详情。该方法不是通用的,只能在类型化数组实例上调用。
示例
测试类型化数组所有元素的大小
下面的示例测试类型化数组所有元素是否都大于等于 10。
function isBigEnough(element, index, array) {
return element >= 10;
}
new Uint8Array([12, 5, 8, 130, 44]).every(isBigEnough); // false
new Uint8Array([12, 54, 18, 130, 44]).every(isBigEnough); // true
规范
浏览器兼容性
参见
core-js中TypedArray.prototype.every的 polyfill- JavaScript 类型化数组指南
- TypedArray
- TypedArray.prototype.forEach()
- TypedArray.prototype.some()
- TypedArray.prototype.find()
- Array.prototype.every()
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
