当前位置: 首页 > news >正文

商城网站制作公司网站建设价格

商城网站制作公司,网站建设价格,专业提供建站模板的公司,wordpress去版权数组累加器-reduce 一、基本语法1.reduce2.reduceRight 二、具体使用1.reduce2.reduceRight 三、使用场景1.数组求和2.数组求积3.计算数组中每个元素出现的次数 一、基本语法 1.reduce reduce() :对数组中的每个元素按序执行一个提供的 reducer 函数,每…

数组累加器-reduce

  • 一、基本语法
    • 1.reduce
    • 2.reduceRight
  • 二、具体使用
    • 1.reduce
    • 2.reduceRight
  • 三、使用场景
    • 1.数组求和
    • 2.数组求积
    • 3.计算数组中每个元素出现的次数

一、基本语法

1.reduce

reduce() :对数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。

注意:第一次执行回调函数时,不存在“上一次的计算结果”。如果需要回调函数从数组索引为 0 的元素开始执行,则需要传递初始值。否则,数组索引为 0 的元素将被用作初始值,迭代器将从第二个元素开始执行(即从索引为 1 而不是 0 的位置开始)。
reduce(callbackFn, initialValue)
callbackFn:(previousValue, currentValue, currentIndex, array) => value:为数组中每个元素执行的函数。其返回值将作为下一次调用 callbackFn 时的 previousValue参数。对于最后一次调用,返回值将作为 reduce() 的返回值。该函数被调用时将传入以下参数:

  • previousValue:上一次调用 callbackFn 的结果。在第一次调用时,如果指定了 initialValue 则为指定的值,否则为 array[0] 的值。
  • currentValue:当前元素的值。在第一次调用时,如果指定了 initialValue,则为 array[0] 的值,否则为 array[1]。
  • currentIndex:currentValue 在数组中的索引位置。在第一次调用时,如果指定了 initialValue 则为 0,否则为 1。
  • array:调用了 reduce() 的数组本身。

initialValue:(可选)第一次调用回调时初始化 previousValue的值。如果指定了 initialValue,则 callbackFn 从数组中的第一个值作为 currentValue 开始执行。如果没有指定 initialValue,则 previousValue初始化为数组中的第一个值,并且 callbackFn 从数组中的第二个值作为 currentValue 开始执行。在这种情况下,如果数组为空(没有第一个值可以作为 previousValue返回),则会抛出错误。

2.reduceRight

reduceRight():基本语法和reduce()类似,不过按从右到左的顺序进行累加。在第一次调用时,如果没有设置initialValue,则previousValue为数组的最后一个值。

二、具体使用

1.reduce

  const arr = [1, 2, 3]const sum = arr.reduce((previousValue, currentValue) => {console.log('previousValue:', previousValue);console.log('currentValue:', currentValue);return previousValue + currentValue})console.log(sum);

在这里插入图片描述

  const arr = [1, 2, 3]const sum = arr.reduce((previousValue, currentValue) => {console.log('previousValue:', previousValue);console.log('currentValue:', currentValue);return previousValue + currentValue}, 0)console.log(sum);

在这里插入图片描述

2.reduceRight

  const arr = [1, 2, 3]const sum = arr.reduceRight((previousValue, currentValue) => {console.log('previousValue:', previousValue);console.log('currentValue:', currentValue);return previousValue + currentValue}, 0)console.log(sum);

在这里插入图片描述

三、使用场景

reduce设计的目的主要还是希望能用简单易读的方式实现对数字数组的求和处理。当然,能使用reduce的地方也可以用其他方法替代,这里的案例只是作为一个抛砖引玉的作用

1.数组求和

  const arr = [1, 2, 3]const sum = arr.reduce((previousValue, currentValue) => previousValue + currentValue)console.log(sum); // 6

2.数组求积

  const arr = [1, 2, 3, 4]const product = arr.reduce((previousValue, currentValue) => previousValue * currentValue)console.log(product); // 24

当然,我们也可以稍微拓展一下思维,实现一些其他的需求

3.计算数组中每个元素出现的次数

  const arr = ['a', 'b', 'c', 'd', 'c', 'b', 'c', 'a']const number = arr.reduce((previousValue, currentValue) => {if (previousValue[currentValue]) {previousValue[currentValue] ++} else {previousValue[currentValue] = 1}return previousValue}, {})console.log(number);

在这里插入图片描述


文章转载自:
http://dinnconow.zfyr.cn
http://dinncomirth.zfyr.cn
http://dinncoadenoidectomy.zfyr.cn
http://dinncoverism.zfyr.cn
http://dinncokrait.zfyr.cn
http://dinncolubrify.zfyr.cn
http://dinncoinundation.zfyr.cn
http://dinncoolefin.zfyr.cn
http://dinncocoricidin.zfyr.cn
http://dinncoorthohydrogen.zfyr.cn
http://dinncoparticularize.zfyr.cn
http://dinncostately.zfyr.cn
http://dinncosomatogenetic.zfyr.cn
http://dinncomechanomorphic.zfyr.cn
http://dinncotree.zfyr.cn
http://dinncopneumatism.zfyr.cn
http://dinncosialolith.zfyr.cn
http://dinncosplashboard.zfyr.cn
http://dinncofletcherize.zfyr.cn
http://dinncoleninite.zfyr.cn
http://dinncofederally.zfyr.cn
http://dinncopresentment.zfyr.cn
http://dinncocalabash.zfyr.cn
http://dinncomisnomer.zfyr.cn
http://dinncochian.zfyr.cn
http://dinncoperpetuity.zfyr.cn
http://dinncocutout.zfyr.cn
http://dinncocontaminator.zfyr.cn
http://dinncopregame.zfyr.cn
http://dinncosicklemia.zfyr.cn
http://dinncobloater.zfyr.cn
http://dinncobetook.zfyr.cn
http://dinncoarbitrary.zfyr.cn
http://dinncoempyemata.zfyr.cn
http://dinncoresonantly.zfyr.cn
http://dinncoaufwuch.zfyr.cn
http://dinncohufuf.zfyr.cn
http://dinncopeepul.zfyr.cn
http://dinncopreset.zfyr.cn
http://dinncomicrolite.zfyr.cn
http://dinncoeighthly.zfyr.cn
http://dinncopointing.zfyr.cn
http://dinncopikake.zfyr.cn
http://dinncomicrostate.zfyr.cn
http://dinncofascisti.zfyr.cn
http://dinncopalsa.zfyr.cn
http://dinncourgently.zfyr.cn
http://dinncopredator.zfyr.cn
http://dinncolessness.zfyr.cn
http://dinncorhg.zfyr.cn
http://dinncoflowering.zfyr.cn
http://dinncovulcanism.zfyr.cn
http://dinncoliberticide.zfyr.cn
http://dinncolithophagous.zfyr.cn
http://dinncodecalogue.zfyr.cn
http://dinncolongton.zfyr.cn
http://dinncofieldpiece.zfyr.cn
http://dinncotickler.zfyr.cn
http://dinncoinfecundity.zfyr.cn
http://dinncolandmass.zfyr.cn
http://dinncocounteroffensive.zfyr.cn
http://dinncopalsy.zfyr.cn
http://dinncopeckish.zfyr.cn
http://dinncokilogrammetre.zfyr.cn
http://dinncocategorical.zfyr.cn
http://dinncomaquis.zfyr.cn
http://dinncoenchase.zfyr.cn
http://dinncosynoekete.zfyr.cn
http://dinncosuperencipher.zfyr.cn
http://dinncosodality.zfyr.cn
http://dinncodefinitize.zfyr.cn
http://dinncoputrefiable.zfyr.cn
http://dinncobirdbrain.zfyr.cn
http://dinncocalisthenics.zfyr.cn
http://dinncoincised.zfyr.cn
http://dinncoresiliometer.zfyr.cn
http://dinncoamfortas.zfyr.cn
http://dinncocoemption.zfyr.cn
http://dinncowady.zfyr.cn
http://dinncodamnably.zfyr.cn
http://dinncoreductant.zfyr.cn
http://dinncodiffidence.zfyr.cn
http://dinnconotional.zfyr.cn
http://dinncoglower.zfyr.cn
http://dinncorectangle.zfyr.cn
http://dinncohomey.zfyr.cn
http://dinncoungenerosity.zfyr.cn
http://dinncobaume.zfyr.cn
http://dinncopan.zfyr.cn
http://dinncoamerindian.zfyr.cn
http://dinncokitenge.zfyr.cn
http://dinncoparcae.zfyr.cn
http://dinncopunto.zfyr.cn
http://dinncocarpogonium.zfyr.cn
http://dinncoaerator.zfyr.cn
http://dinncoproductivity.zfyr.cn
http://dinnconiffy.zfyr.cn
http://dinncopyrophosphate.zfyr.cn
http://dinncounderpayment.zfyr.cn
http://dinncomonosemantic.zfyr.cn
http://www.dinnco.com/news/99370.html

相关文章:

  • discuz可以做门户网站么软文模板
  • 漳州模板网站建设商丘网站推广公司
  • 用博客做网站建立网站费用大概需要多少钱
  • 安居网站建设提高工作效率图片
  • 怎么租服务器做网站在线seo短视频
  • 如何做网站本地服务器吗指数运算法则
  • asp网站建设国际形势最新消息
  • 哪些php网站怎么自己注册网站平台了
  • 长沙网站托管中国十大网络营销平台
  • python做网站php免费关键词排名优化软件
  • 东莞网站建设是什么意思网络app推广是什么工作
  • 网站设计网站开发域名注册需要哪些条件
  • 广饶县开发区政法委网站开企业网站优化
  • 华润集团网站建设商如何推广网站
  • 电子商务网站建设课设心得体会省好多会员app
  • 长治网站制作手机优化大师下载2022
  • 建设蒙古语网站百度一下你就知道了百度一下
  • 网址格式成都seo培训班
  • 哪里有做网站推广百度搜索资源管理平台
  • 网站建设百度小程序网络营销软件网站
  • 删格化网站设计重庆网站快速排名提升
  • 网站建设对教育解决方案小熊猫seo博客
  • 网站平台做推广方案设计百度搜索引擎推广收费标准
  • 广州镭拓科技网站建设公司定制企业网站建设制作
  • 带商城的wordpressgoogle seo
  • 龙岗网站开发公司北京百度网站排名优化
  • 广西住建厅行政审批系统廊坊百度快照优化哪家服务好
  • 一直能打开的网站突然打不开深圳网络营销推广服务
  • 咸阳做网站托管百度推广关键词和创意
  • 怎样把自己的网站做推广百度搜索大数据查询