Array.filter()

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript Array.filter()</h1>
  6.  
  7. <p>使用通过测试的所有数组元素创建一个新数组。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var numbers = [45, 4, 9, 16, 25];
  13. var over18 = numbers.filter(myFunction);
  14.  
  15. document.getElementById("demo").innerHTML = over18;
  16.  
  17. function myFunction(value, index, array) {
  18. return value > 18;
  19. }
  20. </script>
  21.  
  22. </body>
  23. </html>