Array.map()

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript Array.map()</h1>
  6.  
  7. <p>通过对每个数组元素执行函数来创建新数组。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var numbers1 = [45, 4, 9, 16, 25];
  13. var numbers2 = numbers1.map(myFunction);
  14.  
  15. document.getElementById("demo").innerHTML = numbers2;
  16.  
  17. function myFunction(value, index, array) {
  18. return value * 2;
  19. }
  20. </script>
  21.  
  22. </body>
  23. </html>