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 婶婶拜年祝福语大全简短