数组中元素累加 reduce
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>
[65,14,22,5]
</p>
<button onclick="myFunction()">求和</button>
<div id="demo"> </div>
<script>
var numbers=[65,14,22,5];
function getSum(total,num){
return total+num;
}
function myFunction(item){
document.getElementById('demo').innerHTML=numbers.reduce(getSum);
}
</script>
</body>
</html>
reduce()方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
reduce()方法可以作为一个高阶函数,用于函数的compose。
注意:reduce()方法对于空数组是不会执行回调函数的。
语法:
array.reduce( function( total , currentValue , currentIndex , arr) , initialValue)
参数:
function( total, currentValue, currentIndex, arr) 必需。用于执行每个数组元素的函数。
函数参数:
total:必需。初始值,或者计算结束后的返回值。
currentValue:必需。当前元素
currentIndex:可选。当前元素的索引
arr:可选。当前元素所属的数组对象。
initialValue 可选。传递给函数的初始值。
例:
四舍五入后计算数组元素的总和:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<p>
[15.5,2.3,1.1,4.7]
</p>
<button onclick="myFunction()">点我</button>
<p>数组元素之和:<span id="demo"></span></p> <script>
var numbers=[15.5,2.3,1.1,4.7];
function getSum(total,num){
return total+Math.round(num);
}
function myFunction(item){
document.getElementById("demo").innerHTML=numbers.reduce(getSum,0);
}
</script>
</body>
</html>
数组中元素累加 reduce的更多相关文章
- javascript中获取字符串或数组中元素的索引
有些时候,我们需要知道一个字符串中字符的位置,或者一个数组中元素的位置,这是就需要对该变量进行迭代操作. 对于数组,有两个方法indexOf和findIndex() , 需要注意的是,findInde ...
- js删除数组中元素 delete 和splice的区别
例如我有一个数组: var array = ["aa","dd","cc","aa"] ,我想删除这个数组的“dd”元素 ...
- 如何解决jersey框架中以json格式返回数组,当数组中元素一个时json格式不对
原文地址:http://www.cnblogs.com/swpk/p/3566536.html?utm_source=tuicool jersey 是oracle 出的一个较好的REST框架.使用此框 ...
- js删除数组中元素的方法
一.清空数组 var ary = [1,2,3,4]; ary.splice(0,ary.length);//清空数组 console.log(ary); // 输出 [],空数组,即被清空了 二.删 ...
- 使用Arraylist将数组中元素随机均等乱序分为N个子数组
使用Arraylist将数组中元素随机均等乱序分为N个子数组 觉得有用的话,欢迎一起讨论相互学习~Follow Me 为了将数组中的元素 随机地 ,均等地, 不重复地 ,划分到N个子数组中 使用Arr ...
- 根据Bool值挑选数组中元素
根据Bool值挑选数组中元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用Boolean类型的数组挑选一维数组中的值 使用一维Boolean数组选取数组中的特定元素,对应位置为True ...
- JavaScript Array reverse 方法:颠倒数组中元素的顺序
在JavaScript中,Array对象的reverse()方法将颠倒(反转)数组中元素的顺序.arr.reverse()在原数组上实现这一功能,即,reverse()会改变原数组. 例1:将数组元素 ...
- 黑马基础阶段测试题:定义一个int类型的数组,数组中元素为{5,7,3,9,4}。求出数组中的最小值,并判断最小值是否为偶数,如果是偶数则输出“最小值为偶数”,如果不是偶数则输出“最小值为奇数”。打印如下:
package com.swift; import java.util.Arrays; public class ArrayTest { public static void main(String[ ...
- JS中split使用方法和数组中元素的删除
JS中split使用方法和数组中元素的删除 JS中split使用方法 <script language="javascript"> function spli(){ d ...
随机推荐
- sqrt函数
import numpy as np B = np.arange(3) print (B) print (np.sqrt(B)) #求平方根
- 测试一下robotgo自动化操作,顺便解决了原来的mingw版本中只有gcc,没有g++的问题
参考:https://gitee.com/veni0/robotgo#examples 但是编译不成功 找到这个:https://gitee.com/veni0/robotgo#examples ( ...
- Python之Requests库的7个主要方法
方法 说明 requests.request() 构造一个请求,支撑一下各方法的基础方法 requests.get() 获取HTML网页的主要方法 requests.head() 获取网页头信息的方法 ...
- [matlab] 15.罚函数降维
求非线性规划 min f(x)= x(1)^2 + x(2)^2 + 8 s.t. { x(1)^2-x(2)>=0 , -x(1) - x(2)^2 +2 = 0, x(1)>=0 ,x ...
- 解决Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
异常: Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj ...
- 根据JavaBean创建数据库的操作SQL
根据JavaBean创建数据库的操作SQL import java.lang.reflect.Field; public class GenerateSQL { public static void ...
- Nginx缓存服务
Nginx缓存服务 1.缓存常见类型 2.缓存配置语法 3.缓存配置实践 4.缓存清理实践 5.部分页面不缓存 6.缓存日志记录统计 通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后 ...
- utc时间转成local时间
public static Date utcToLocal(String utcTime){ SimpleDateFormat sdf = new SimpleDateFormat("yyy ...
- hyperledger中文文档学习-1-词汇表
参考:https://hyperledgercn.github.io/hyperledgerDocs/glossary/ Chaincode - 链码,即智能合约 链码是一个运行在账本上的软件,它可以 ...
- shell去除换行和空格
#!/bin/bash if [ -f str.txt ] ## 如果str.txt存在,则返回true then strval=$(cat str.txt|awk '{printf "%s ...