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 给外婆敬酒祝福语简短