map &forEach
var array1 = [1,4,9,16];
const map1 = array1.map(x => x *2);
console.log(map1);
打印结果为:
Array [2,8,18,32]
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <button onclick="numbers.forEach(myFunction)">点我</button> <p>计算后的值: <span id="demo"></span></p> <script> var numbers = [1, 2,3, 4]; function myFunction(item,index,arr) { console.log(arr); arr[index] = item*2; demo.innerHTML = numbers; } </script> </body> </html>