JavaScript |类型Darray。findIndex()及其示例

这个 达雷。findIndex() 是JavaScript中的一个内置函数,如果值满足函数中给定的条件,则用于返回TyeArray中的索引,否则返回-1。 语法:

null
typedarray.findIndex(callback)

参数: 它采用参数“callback”函数,该函数检查所提供条件满足的typedArray的每个元素。回调函数接受下面指定的三个参数-

  • 要素: 它是元素的值。
  • 索引: 它是元素的索引。
  • 数组: 正在遍历的是数组。

    返回值: 如果元素满足函数提供的条件,则返回数组中的索引,否则返回-1。 显示此函数工作的JavaScript代码:

    <script>
    // Calling isNegative function to check
    // elements of the typedArray
    function isNegative(element, index, array)
    {
    return element < 0;
    }
    // Created some typedArrays.
    const A = new Int8Array([ -10, 20, -30, 40, -50 ]);
    const B = new Int8Array([ 10, 20, -30, 40, -50 ]);
    const C = new Int8Array([ -10, 20, -30, 40, 50 ]);
    const D = new Int8Array([ 10, 20, 30, 40, 50 ]);
    // Calling findIndex() function to check condition
    // provided by its parameter
    const a = A.findIndex(isNegative);
    const b = B.findIndex(isNegative);
    const c = C.findIndex(isNegative);
    const d = D.findIndex(isNegative);
    // Printing the indexes typedArray
    document.write(a + "<br>" );
    document.write(b + "<br>" );
    document.write(c + "<br>" );
    document.write(d);
    </script>

    
    

    输出:

    0
    2
    0
    -1
  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞6 分享