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

番禺做网站公司哪家好刷关键词排名软件

番禺做网站公司哪家好,刷关键词排名软件,设计师培训心得体会,图书馆网站建设策划书一、语法 array.reduce(callbackfn, initialValue);callbackfn (accumulator, currentValue, currentIndex, array) > {// 回调逻辑 } callbackFn 为数组中每个元素执行的函数。 其返回值将作为下一次调用 callbackFn 时的 accumulator 参数。对于最后一次调用&#xff0c…

一、语法

array.reduce(callbackfn, initialValue);callbackfn =(accumulator, currentValue, currentIndex, array) => {// 回调逻辑
}
callbackFn

为数组中每个元素执行的函数。

其返回值将作为下一次调用 callbackFn 时的 accumulator 参数。对于最后一次调用,返回值将作为 reduce() 的返回值。

currentValue

当前元素的值。

在第一次调用时,若指定了 initialValue,则为 array[0] 的值,否则为 array[1]。(若未指定 initialValue, array[0] 的值将充当initialValue

currentIndex

currentValue 在数组中的索引位置。

在第一次调用时,如果指定了 initialValue 则为 0,否则为 1

 initialValue

第一次调用回调时初始化 accumulator 的值。

如果指定了 initialValue,则 callbackFn 从数组中的第一个值(即 array[0])作为 currentValue 开始执行。如果没有指定 initialValue,则 accumulator 初始化为数组中的第一个值,并且 callbackFn 从数组中的第二个值(即 array[1])作为 currentValue 开始执行。在这种情况下,如果数组为空(没有第一个值可以作为 accumulator 返回),则会抛出错误。

二、示例

1、打印所有参数

const numbers = [10, 20, 30];numbers.reduce((acc, curr, idx, arr) => {console.log({accumulator: acc,currentValue: curr,currentIndex: idx,array: arr,});return acc + curr; // 累加求和
}, 0);
{accumulator: 0,       // 初始值currentValue: 10,     // 第一个元素currentIndex: 0,      // 第一个索引array: [10, 20, 30]   // 原数组
}
{accumulator: 10,      // 上一轮返回值 (0 + 10)currentValue: 20,     // 第二个元素currentIndex: 1,      // 第二个索引array: [10, 20, 30]
}
{accumulator: 30,      // 上一轮返回值 (10 + 20)currentValue: 30,     // 第三个元素currentIndex: 2,      // 第三个索引array: [10, 20, 30]
}

最终返回值:6010 + 20 + 30)。

 二、统计数组中每个元素的出现次数,并返回一个对象(键为元素值,值为出现次数)

const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];const countedNames = names.reduce((allNames, name) => {const currCount = allNames[name] ?? 0; // 获取当前名字的计数(若不存在则为 0)return {...allNames, // 复制原对象的所有属性[name]: currCount + 1, // 更新当前名字的计数};
}, {}); // 初始值为空对象 {}
第一轮
  • allNames = {}(初始值)
  • name = "Alice"
  • currCount = allNames["Alice"] ?? 0 → undefined ?? 0 → 0
  • 返回:

    { ...{}, "Alice": 0 + 1 }

    → { "Alice": 1 }
第二轮
  • allNames = { "Alice": 1 }
  • name = "Bob"
  • currCount = allNames["Bob"] ?? 0 → undefined ?? 0 → 0
  • 返回:

    { ...{ "Alice": 1 }, "Bob": 0 + 1 }

    → { "Alice": 1, "Bob": 1 }
第三轮
  • allNames = { "Alice": 1, "Bob": 1 }
  • name = "Tiff"
  • currCount = allNames["Tiff"] ?? 0 → undefined ?? 0 → 0
  • 返回:

    { ...{ "Alice": 1, "Bob": 1 }, "Tiff": 0 + 1 }

    → { "Alice": 1, "Bob": 1, "Tiff": 1 }
第四轮
  • allNames = { "Alice": 1, "Bob": 1, "Tiff": 1 }
  • name = "Bruce"
  • currCount = allNames["Bruce"] ?? 0 → undefined ?? 0 → 0
  • 返回:

    { ...{ "Alice": 1, "Bob": 1, "Tiff": 1 }, "Bruce": 0 + 1 }

    → { "Alice": 1, "Bob": 1, "Tiff": 1, "Bruce": 1 }
第五轮
  • allNames = { "Alice": 1, "Bob": 1, "Tiff": 1, "Bruce": 1 }
  • name = "Alice"
  • currCount = allNames["Alice"] ?? 0 → 1 ?? 0 → 1
  • 返回:

    { ...{ "Alice": 1, "Bob": 1, "Tiff": 1, "Bruce": 1 }, "Alice": 1 + 1 }

    → { "Alice": 2, "Bob": 1, "Tiff": 1, "Bruce": 1 }

最终结果

{ "Alice": 2, "Bob": 1, "Tiff": 1, "Bruce": 1 }

关键点

  1. ?? 运算符(空值合并运算符)​

    (1)如果左侧操作数为 null 或 undefined,返回右侧操作数;否则返回左侧操作数。(2)在这里用于处理名字第一次出现时 allNames[name] 为 undefined 的情况。
  2. ​展开运算符 ...

    复制原对象的所有属性,避免直接修改原对象(保持不可变性)。
  3. ​动态键名 [name]

    使用计算属性名语法,将变量 name 的值作为对象的键。
  4. ​初始值 {} 的作用​

    确保累加器从空对象开始,避免未定义行为。

三、题目

1991. 找到数组的中间位置 - 力扣(LeetCode)

数组和字符串 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台

http://www.dtcms.com/wzjs/418373.html

相关文章:

  • ps做图游戏下载网站有哪些百度竞价登陆
  • 广州市建设工程造价站网站seo自学网免费
  • 旧房翻新装修哪家公司好优化网站的步骤
  • vue网站引导页怎么做西安网站建设制作公司
  • 湖南常德广宇建设网站竞价推广运营
  • 宿松 做网站关键词免费下载
  • 青岛官网排名推广优化设计电子版
  • 做视频网站需要什么条件青岛seo推广公司
  • 汽车业务网站开发公司百度蜘蛛池自动收录seo
  • 网页怎么制作超链接网站快速优化排名官网
  • 坡头手机网站建设seo初学教程
  • 宜兴市做网站实时热搜榜
  • wordpress主题栏是什么意思seo网站关键词优化怎么做
  • 网站后台清除缓存在哪南宁seo专员
  • 机关网站建设管理工作自查报告2022年度最火关键词
  • 太原自助建站软件seo站外推广
  • 郴州公司做网站软文写作的十大技巧
  • 做网站包括什么东莞网站推广运营公司
  • 独立网站制作关键词排名优化软件策略
  • 网站开发设备费用计入什么科目网站申请流程
  • 只买域名怎么做网站网络营销方案策划
  • 怎么看网站做没做seo中国最大的企业培训公司
  • 网站后台管理 源码怎样免费建立自己的网站
  • 用自己的电脑做网站空间淘宝怎么优化关键词排名
  • 好公司网站建设seo排名推广工具
  • 黄骅市海边惠州seo代理商
  • 建设部网站燃气管理部门云南seo公司
  • 网站建设 发展方向优化大师官方免费
  • 商城网站开发方案百度seo关键词优化电话
  • 大眼睛网站建设百度快照优化公司