当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript TypedArray.every()用法及代码示例


TypedArray 的 every() 函数接受一个表示函数名称的字符串值,测试数组中的所有元素是否通过提供的函数实现的测试。

用法

其语法如下

typedArray.every(function_name)

示例

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([64, 89, 65,21, 14, 66, 87, 55 ]);
      document.write("Contents of the typed array:"+int32View);
      document.write("<br>");
      function testResult(element, index, array) {
         var ele = element>35
         return ele;
      }
      result = int32View.every(testResult);
      if(result) {
         document.write("all elements are above 35");
      }else {
         document.write("all elements are not above 35");
      }
      </script>
</body>
</html>

输出

Contents of the typed array:64,89,65,21,14,66,87,55
all elements are not above 35

示例

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([64, 89, 65, 58, 90, 66, 87, 55 ]);
      document.write("Contents of the typed array:"+int32View);
      document.write("<br>");
      function testResult(element, index, array) {
         var ele = element>35
         return ele;
      }
      result = int32View.every(testResult);
      if(result) {
         document.write("all elements are above 35");
      }else {
         document.write("all elements are not above 35");
      }
   </script>
</body>
</html>

输出

Contents of the typed array:64,89,65,58,90,66,87,55
all elements are above 35

相关用法


注:本文由纯净天空筛选整理自Karthikeya Boyini大神的英文原创作品 TypedArray.every() function in JavaScript。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。