Array.reduce()
- <!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 = "总和是:" + sum;
- function myFunction(total, value, index, array) {
- return total + value;
- }
- </script>
- </body>
- </html>