JavaScript中Array.Reduce()方法的用途是什么?
Array.Reduce()
Array.Reduce()方法对数组的每个元素执行提供给用户的函数(由用户提供),并给出单个输出。在给定的示例中,提供的函数(由用户提供的函数为加法)在每个元素上执行通过返回单个输出(10)获得数组(1,2,3,4)的数组。
示例
<html> <body> <p>Sum of numbers in array: </p> <script> const array = [1, 2, 3, 4]; const sum = (firstValue, lastValue) => firstValue + lastValue; document.write(array.reduce(sum)); </script> </body> </html>
输出结果
Sum of numbers in array:10
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短