Array.reduce()学习
昨天遇到的一道题:1234567890 => 1,234,567,890 要求其实就是使用逗号做千分位将数字进行分隔。
当时没想到任何方法,但是以前看到过,印象很深刻,今天就找了一下。
看到其实现方法是使用Array.reduce()方法:
var str = 1234567890 + '';
str.split('').reverse().reduce((prev,next,index) => {
return ((index % 3) ? next : (next + ',')) + prev;
})
由于对reduce方法不够熟悉,恶补了一下,下面总结一下:
语法:Array.reduce(calback[, initValue])
reduce方法接受两个参数,第一个参数为对数组每个元素处理的回调方法,第二个参数可选,为reduce的初始值,如果没有设置初始值,则使用数组第一个元素。注意:在对没有设置初始值的空数组调用reduce方法时会报错。
回调参数callback的参数:accumulator,currentValue,currentIndex,array。
解释一下这几个参数的意思:
accumulator:数组累计操作的返回值,是上一次成功操作的返回值或初始值。
currentValue:当前操作的值。
currentIndex:当前操作的索引,有初始值为0,否则为1。
array:操作的原数组。
*回调函数第一次执行时,accumulator和currentValue的取值有两种情况:有初始值时,accumulator的取值为初始值,currentValue取值为数组第一个元素。无初始值时,accumulator的取值为数组第一个元素,currentValue取值为数组第二个元素。
下面是reduce方法的运行片段:
//无初始值
[1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array){
return accumulator + currentValue;
})
| callback | accumulator | currentValue | currentIndex | array | return value |
| first call | 1(数组第一个元素) | 2(数组第二个元素) | 1(无初始值为1) | [1, 2, 3, 4] | 3 |
| second call | 3 | 3 | 2 | [1, 2, 3, 4] | 6 |
| third call | 6 | 4 | 3 | [1, 2, 3, 4] | 10 |
//有初始值
[1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array){
return accumulator + currentValue;
}, 10)
| callback | accumulator | currentValue | currentIndex | array | return value |
| first call | 10(初始值) | 1(数组第一个元素) | 0(有初始值为0) | [1, 2, 3, 4] | 11 |
| second call | 11 | 2 | 1 | [1, 2, 3, 4] | 13 |
| third call | 13 | 3 | 2 | [1, 2, 3, 4] | 16 |
| fourth call | 16 | 4 | 3 | [1, 2, 3, 4] | 20 |
一些例子:
//1.数组元素求和
[1, 2, 3, 4].reduce((a, b) => a+b); // //2.二维数组转化为一维数组
[[1, 2], [3, 4], [5, 6]].reduce((a, b) => a.concat(b), []) //[1, 2, 3, 4, 5, 6] //3.计算数组中元素出现的次数
[1, 2, 3, 1, 2, 3, 4].reduce((items, item) => {
if(item in items){
items[item]++;
}else{
items[item] = 1;
}
return items;
},{}) //{1: 2, 2: 2, 3: 2, 4: 1} //数组去重①
[1, 2, 3, 1, 2, 3, 4, 4, 5].reduce((init, current) => {
if(init.length === 0 || init.indexOf(current) === -1){
init.push(current);
}
return init;
},[]) //[1, 2, 3, 4, 5]
//数组去重②
[1, 2, 3, 1, 2, 3, 4, 4, 5].sort().reduce((init, current) => {
if(init.length === 0 || init[init.length-1] !== current){
init.push(current);
}
return init;
},[]) //[1, 2, 3, 4, 5]
Array.reduce()学习的更多相关文章
- javascript Array 方法学习
原生对象Array学习 Array.from() 从类似数组的对象或可迭代的对象返回一个数组 参数列表 arraylike 类似数组的对象或者可以迭代的对象 mapfn(可选) 对对象遍历映 ...
- Array.reduce()方法的使用
起因是学习异步函数的串行与并行写法时,发现reduce方法可以简化写法,然后看到一篇博客里面这样一段代码: var array = [1, [2, [3, 4], 5], 6]; function f ...
- 自从学会了 Array.reduce() ,再也离不开它
(转载)原文链接:https://juejin.im/post/5dfd9d27e51d455825129ec3 在所有后 ES6 时代的数组方法中,我觉得最难理解的就是Array.reduce( ...
- Js中Array数组学习总结
第一次写博客...有点方... 小白一枚(是真的小白),自学前端,下面来说说我在学习过程中总结的一些数组操作,如果说哪有错误,请各位大神多多指出,小的虚心接受. 引用类型分为Object类型(所谓的对 ...
- 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
有一道经典的字符串处理的问题,统计一个字符串中每个字符出现的次数. 用es6的Array.reduce()函数配合“...”扩展符号可以更方便的处理该问题. s='abananbaacnncn' [. ...
- JS Array.reduce 对象属性累加
Array reduce() 方法 ,无非就是 计算数组元素 相加后的总和 ,看网上给的Demo 全是 [1,2,3,4,6].reduce 这种基本用法, 本次我将使用 reduce 实现 数组 ...
- Array.reduce()方法
Array.reduce()方法是对数组的遍历,返回一个单个返回值 使用方法: Array.reduce((acc, cur, idx, src) => { }, initialValue) ...
- JavaScrip中 Array.reduce()
数组的方法 reduce() reduce方法在数组的每一项元素上都会执行回调函数. 语法:array.reduce( callBack [ , init] ) // 语法arrary.reduce ...
- javascript Array Methods(学习笔记)
ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach(); 2.map(); 3.filter(); 4.every(); 5.some(); 6.reduce() ...
随机推荐
- C# Ioc ASP.NET MVC Dependency Injection
ASP.NET MVC Dependency Injection 同志们,非常快速的Ioc注册接口和注入Mvc Controller,步骤如下: 安装Unity.Mvc NuGet Package 在 ...
- 浅谈OSI七层模型及ICP/IP四层模型
1.OSI七层模型的概念 在网络历史的早期,国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)共同出版了开放系统互联的七层参考模型. 一台计算机操作系统中的网络过程包括从应用请求(在协议栈 ...
- Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器
本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...
- apache配置详解
可参考:Apache 的 httpd.conf 详解 ServerTokens OS 此指令控制了Server回送给客户端的回应头域是否包含关于服务器OS类型和编译进的模块描述信息.服务器会发送:Se ...
- make和makefile简明基础
0.make.makefile是什么? makefile定义了一系列的规则,来规定哪些部分先编译,哪些部分后编译,写好makefile以后,只需一个make命令就可以让整个工程完全自动编译,所以简单的 ...
- Spring基础篇——通过Java注解和XML配置装配bean
自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...
- 关于webconsole报../website/console.go:35: undefined: ssh.InsecureIgnoreHostkey 错误解决方案
1.首先,进入webconsole目录删除/opt/webconsole/src/golang.org/x/目录下 crypto文件夹 2.然后,在/opt/webconsole/src/golang ...
- gitlab项目迁移
ALL Git* => Gitlab Nothing, Just copy the git URL to gitlab(类似于 fork) 使用 Git Mirror 無痛轉移 Git Serv ...
- 在Linux/Centos下用wondershaper限速
wondershaper是国外人开发的一款在Linux内核下基于TC工具的对整块网卡的限度工具,虽然有很久没有更新了,但是测试老版本在Centos6.3上依然可以使用. 首先下载wondershape ...
- 在tableViewCell的点击事件中处理界面跳转问题
UIViewController *controller; UIView *view = self.view; while (1) { controller = (UIViewController * ...