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 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短