JavaScript |类型Darray。每个()都有例子

这个 达雷。每 函数是JavaScript中的一个内置函数,用于测试typedArray中的元素是否满足函数提供的条件。 语法:

null
typedarray.every(callback)

参数: 它以回调函数作为参数。 回调函数用于测试typedArray的元素。此回调函数接受下面指定的三个参数-

  • 当前_值: 它是typedArray中正在处理的当前元素。
  • 索引: 它是类型化数组中正在处理的当前元素的索引。
  • 数组: 这是Darray的型号。

    返回值: 如果函数回调为typedArray中的每个数组元素返回true值,则返回true,否则返回false。

    显示此函数工作的JavaScript代码:
    代码#1:

    <script>
    // is_negative function is called to test the
    // elements of the typedArray element.
    function is_negative(current_value, index, array)
    {
    return current_value < 0;
    }
    // Creating a typedArray with some elements
    const A = new Int8Array([ -5, -10, -15, -20, -25, -30 ]);
    // Printing whether elements are satisfied by the
    // functions or not
    document.write(A.every(is_negative));
    </script>

    
    

    输出:

    true

    代码#2:

    <script>
    // is_negative function is called to test the
    // elements of the typedArray element.
    function is_positive(current_value, index, array)
    {
    return current_value > 0;
    }
    // Creating a typedArray with some elements
    const A = new Int8Array([ -5, -10, -15, -20, -25, -30 ]);
    // Printing whether elements are satisfied by the
    // functions or not
    document.write(A.every(is_positive));
    </script>

    
    

    输出:

    false
  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞5 分享