Array.findIndex()

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript Array.findIndex()</h1>
  6.  
  7. <p id="demo"></p>
  8.  
  9. <script>
  10. var numbers = [4, 9, 16, 25, 29];
  11. var first = numbers.findIndex(myFunction);
  12.  
  13. document.getElementById("demo").innerHTML = "大于 18 的第一个值的索引是:" + first;
  14.  
  15. function myFunction(value, index, array) {
  16. return value > 18;
  17. }
  18. </script>
  19.  
  20. </body>
  21. </html>