wordpress建站吧,建立电影网站教程,网站开发方向和移动开发方向那个好,wordpress使用云数据库连接ts#xff1a;数组的常用方法#xff08;reduce累加#xff09; 一、主要内容说明二、例子reduce方法#xff08;累加#xff09;1.源码1 #xff08;reduce方法#xff09;2.源码1运行效果 三、结语四、定位日期 一、主要内容说明
ts中数组的reduce方法#xff0c;用… ts数组的常用方法reduce累加 一、主要内容说明二、例子reduce方法累加1.源码1 reduce方法2.源码1运行效果 三、结语四、定位日期 一、主要内容说明
ts中数组的reduce方法用于数组元素的累加。reduce方法可设置的5个参数如下
let arr:string[];
let new_arrarr.reduce((A,B,C,D),E);A----累加值保存上一次调用回调函数的返回值或初始值B----当前正在处理的数组中的某个元素C----(可选)当前处理的数组元素的索引C----(可选)源数组E----(可选)初始值可自行设置的初始值参数若没有设置以数组中的第一个元素为初始值。一般情况下我们只用到A和B这两个参数。使用箭头函数输入A、B对象对A、B对象进行函数逻辑变换若求总和值可如下设置:
let arr:string[];
let new_arrarr.reduce((A,B)AB,10);A是不断累加的总和B为当前运行排到的元素。上例我们设置的初始值为10那么第一轮的总和数值为 A110B1这个值就会累加到A上。到了第二轮那么就是A2[A110B1]B2以此类推A可以累加输出所有的数值。初始值为自己设置和数组元素类型相同的参数也可不设置而默认选用数组第一个元素的值。reduce方法和下边循环累加逻辑思想很相似。
let sum 0;
for (i 0; i 10; i) {sum sum i;}二、例子
reduce方法累加
当数组中的元素为number一类我们可以用reduce累加元素数值。若原数组为string字符串类型用reduce累加则会使原数组中的各个小字符串元素合并成为一个大的字符串。也可以使用对其他类型的对象函数中的某些值进行累加。
1.源码1 reduce方法
// 数字数组
// 数字数组
const numbers: number[] [764, 765, 766, 767];// 使用 reduce 计算数组中所有数字的总和未设置初始值
const sum_1 numbers.reduce((A, B) A B);
console.log(sum_1); // 输出3062// 使用 reduce 计算数组中所有数字的总和并设置初始值为 1000000
const sum_2 numbers.reduce((sum, num) sum num, 1000000);
console.log(sum_2); // 输出1003062console.log(\n分隔行------------------------------\n);// 字符串数组
const str: string[] [元素1, 元素2, 元素3, 元素4, 元素5, 元素6];// 使用 reduce 连接字符串并设置初始值
const sum_3 str.reduce((A, B) A -- B, 如果设置起始元素);
console.log(sum_3); // 输出如果设置起始元素元素1--元素2--元素3--元素4--元素5--元素6
console.log(原数组 str);// 原数组console.log(\n分隔行------------------------------\n);// 定义 Message 接口
interface Message {name: string;num: number;
}// 消息数组
const mes: Message[] [{ name: 桃子, num: 764 },{ name: 苹果, num: 765 },{ name: 猕猴桃, num: 766 },{ name: 西瓜, num: 767 }
];// 使用 reduce 计算水果数量总和初始值为 0
const mes_2 mes.reduce((sum, message) sum message.num, 0); // 从 0 开始累加数量// 输出结果
console.log(所有水果总和 mes_2); // 输出所有水果总和3062
2.源码1运行效果 三、结语
每日过一遍学过的内容写下学过方面的知识印象也会深些。其实不是很难的内容但为啥完成一篇博文也会花费许多时间搞不明白。 类class的简单创建已经简单过一遍类里面的对象变量可设置为公共、私有等的内容还有继承和public静态类的设置。其实可选内容也挺多但有时候也难以下手编写。总之加油吧 由于笔者的能力有限创作的内容有所不足在所难免也敬请读者包涵和指出万分感谢
四、定位日期
2024:10:28 21:20 文章转载自: http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.sqfrg.cn.gov.cn.sqfrg.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.qrqdr.cn.gov.cn.qrqdr.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.czzpm.cn.gov.cn.czzpm.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn