当前位置:网站首页>js reduce
js reduce
2022-08-10 07:49:00 【Ares-Wang】
// 第一种:闭包存储,遍历执行
function compose1 (arr) {
return (param) => {
for (let fn of arr) {
param = fn(param);
}
return param;
}
}
// 第二种:递归嵌套,依次执行
function compose2 (arr) {
return arr.length <= 1 ? arr[0] : (...args) => compose2(arr.slice(1))(arr[0](...args))
}
// 第三种:累加嵌套,依次执行,跟第二种相似
const compose3 = (arr) => arr.reduce((res, cur) => (...args) => cur(res(...args)));
function a (p) {
return p + 1;
}
function b (p) {
return p + 6;
}
function c (p) {
return p * 2;
}
// 第四种:注意与第三章的区别
const compose4 = (arr) => arr.reduce((res, cur) => (...args) => res(cur(...args)));
const fn = compose4([a, b,c,d])
console.log(fn(1))
arr.reduce(callback,[initialValue])
reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce 的数组。
callback (执行数组中每个值的函数,包含四个参数)
1、previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue))
2、currentValue (数组中当前被处理的元素)
3、index (当前元素在数组中的索引)
4、array (调用 reduce 的数组)
initialValue (作为第一次调用 callback 的第一个参数。)
var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
console.log(prev, cur, index);
return prev + cur;
})
console.log(arr, sum);
打印结果:
1 2 1
3 3 2
6 4 3
[1, 2, 3, 4] 10
这里可以看出,上面的例子index是从1开始的,第一次的prev的值是数组的第一个值。数组长度是4,但是reduce函数循环3次。
var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
console.log(prev, cur, index);
return prev + cur;
},0) //注意这里设置了初始值
console.log(arr, sum);
打印结果:
0 1 0
1 2 1
3 3 2
6 4 3
[1, 2, 3, 4] 10
这个例子index是从0开始的,第一次的prev的值是我们设置的初始值0,数组长度是4,reduce函数循环4次。
结论:如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供initialValue,从索引0开始。
注意:如果这个数组为空,运用reduce是什么情况?
var arr = [];
var sum = arr.reduce(function(prev, cur, index, arr) {
console.log(prev, cur, index);
return prev + cur;
})
//报错,“TypeError: Reduce of empty array with no initial value”
但是要是我们设置了初始值就不会报错,如下:
var arr = [];
var sum = arr.reduce(function(prev, cur, index, arr) {
console.log(prev, cur, index);
return prev + cur;
},0)
console.log(arr, sum); // [] 0
所以一般来说我们提供初始值通常更安全
(1)计算数组中每个元素出现的次数
let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
let nameNum = names.reduce((pre,cur)=>{
if(cur in pre){
pre[cur]++
}else{
pre[cur] = 1
}
return pre
},{
})
console.log(nameNum); //{Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}
(2)数组去重
let arr = [1,2,3,4,4,1]
let newArr = arr.reduce((pre,cur)=>{
if(!pre.includes(cur)){
return pre.concat(cur)
}else{
return pre
}
},[])
console.log(newArr);// [1, 2, 3, 4]
(3)将二维数组转化为一维
let arr = [[0, 1], [2, 3], [4, 5]]
let newArr = arr.reduce((pre,cur)=>{
return pre.concat(cur)
},[])
console.log(newArr); // [0, 1, 2, 3, 4, 5]
(4)、对象里的属性求和
var result = [
{
subject: 'math',
score: 10
},
{
subject: 'chinese',
score: 20
},
{
subject: 'english',
score: 30
}
];
var sum = result.reduce(function(prev, cur) {
return cur.score + prev;
}, 0);
console.log(sum) //60
边栏推荐
- Introduction to the C language to realize bubble sort
- VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定
- 一文2600字手把手教你编写性能测试用例
- Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
- AFNetworking概述和4.0的实践
- 颜色选择器的使用
- 2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
- Introduction to C integer data storage
- 快速输入当前日期与时间
- day16--The use of the packet capture tool Charles
猜你喜欢
随机推荐
Introduction to the delta method
Fiddler(八) - 抓取手机APP的流量-插件Fiddler Orchestra Beta安装&配置
神经网络的三种训练方法,神经网络训练全过程
day16--The use of the packet capture tool Charles
uni 小程序腾讯地图polygon背景透明度
基于STC8G2K64S4单片机通过OLED屏幕显示模拟量光敏模拟值
Discussion on Chinese Fuzzy Retrieval in Databases
阿里云数据库 RDS SQL Server 版的服务器绑定域名www.cxsdkt.cn.的呢?
In the SQL SERVER database, if the data of the table is added, deleted, or modified, will the index of the table be recorded in the ldf log?
添加spark的相关依赖和打包插件(第六弹)
day16--抓包工具Charles的使用
nrm 使用详解
【MySQL】SQL语句
大佬,oracle单表增量同步时候源库服务器额外占用内存近2g,这不正常吧
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
Class Notes (7) (1) - #647. Find the root and the child (root)
第2章 变量和基本类型读书笔记
自动化测试框架搭建 ---- 标记性能较差用例
.NET-8.我的思想笔记
【NeRF】原始论文解读