JavaScript |类型Darray。使用示例查找()

这个 达雷。查找() 是JavaScript中的一个内置函数,用于返回typedArray中的值,如果该值满足函数中给定的条件,则返回undefined。

null

语法:

typedArray.find(callback)

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

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

    返回值: 如果元素满足函数提供的条件,则返回数组的值,否则返回undefined。

    显示此函数工作的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 find() function to check condition
    // provided by its parameter
    const a = A.find(isNegative);
    const b = B.find(isNegative);
    const c = C.find(isNegative);
    const d = D.find(isNegative);
    // Printing the finded typedArray
    document.write(a + "<br>" );
    document.write(b + "<br>" );
    document.write(c + "<br>" );
    document.write(d);
    </script>

    
    

    输出:

    -10
    -30
    -10
    undefined
  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞9 分享