W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript Array.reduce()</h1> <p>此例确定数组中所有数字的总和:</p> <p id="demo"></p> <script> var numbers = [45, 4, 9, 16, 25]; var sum = numbers.reduce(myFunction); document.getElementById("demo").innerHTML = "The sum is " + sum; function myFunction(total, value, index, array) { return total + value; } </script> </body> </html>