当前位置:网站首页>比你想象中更强大的 reduce 以及对敲码的思考
比你想象中更强大的 reduce 以及对敲码的思考
2022-06-10 00:59:00 【zz_jesse】
前言
前面的这篇文章 JS 基础\! | 扁平数组和JSON树的转换[2] 利用到了 reduce来实现数组转为map,以及结合concat实现数组递归拼接。今天我们来看看还能搞些什么名堂~
简单复习一下 reduce
语法
let value = arr.reduce(function(previousValue, item, index, array) {
// ...
}, [initial]);
复制代码参数:
previousValue: 上一个函数调用的结果,第一次等于initial(如果提供了initial的话)。item: 当前的数组元素。index:当前索引。arr: 数组本身。
previousValue实际上有点像累加,所以一些地方也会叫将这个参数称为accumulator ,存储前面所有的执行结果,最后会成为reduce的结果。
可读性
但通常,你一般是不会完全参照这些参数定义的意思来决定参数名称,而是要看具体开发遇到的场景来给其取可读性高的名称。
部分应用
1. 经典累加器
将数组中的值从左到右累加,大家学reduce的时候第一个例子应该就是这个。这里只是简单的提一下,不是本文的重点~
const a = [1, 2, 8, 7, 4];
const sum = a.reduce((pre, cur) => {
const res = pre + cur;
return res;
}, 0);
console.log(sum); // 22
复制代码2. 二维数组转一维
结合 concat 实现数组扁平化
const arr2 = [
[1, 2],
[3, 4],
[5, 6],
].reduce((acc, cur) => {
return acc.concat(cur);
}, []);
console.log(arr2); //
复制代码3. 多维数组扁平
这个算是上一个的进阶和更为通用的版本~
这篇文章[3]里就用到了类似的,结合 concat 和递归
const arr3 = [
[1, 2],
[3, 4],
[5, [7, [9, 10], 8], 6],
];
const flatten = arr =>
arr.reduce(
(pre, cur) => pre.concat(Array.isArray(cur) ? flatten(cur) : cur),
[]
);
console.log(flatten(arr3)); // [ 1, 2, 3, 4, 5, 7, 9, 10, 8, 6 ]
复制代码4. 数组分块
根据传入限制大小,对数组进行分块。
小于限制长度时就往里添加,否则直接将其加入res
const chunk = (arr, size) => {
return arr.reduce(
(res, cur) => (
res[res.length - 1].length < size
? res[res.length - 1].push(cur)
: res.push([cur]),
res
),
[[]]
);
};
const arr4 = [1, 2, 3, 4, 5, 6];
console.log(chunk(arr4, 3)); // [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
复制代码5. 字符统计
统计文本中各个字的数量
const countChar = text => {
text = text.split("");
return text.reduce((record, c) => {
record[c] = (record[c] || 0) + 1;
return record;
}, {});
};
const text = "划水水摸鱼鱼";
console.log(countChar(text)); // { '划': 1, '水': 2, '摸': 1, '鱼': 2 }
复制代码思考
本文只讲了几个应用,其实还有更多的应用,以及一些实现 JS API 的功能,比如
数组填充
求最大、最小值
reverse
map
...
等等,这些代码段的整合网上已经有够多了,我这里想总结一下写出这些实用、通用的代码需要进行怎样的思考
core-js
其实从 Polyfill ofArray.prototype.reduceincore-js[4] 中可以从另一个角度理解 reduce 中第一个参数回调函数中接收的第一个参数:
class Array {
//...
reduce(callbackfn: (memo: any, value: any, index: number, target: any) => any, initialValue?: any): any;
//...
}
复制代码他这里用的是 memo,中文直接翻译过来就是备忘录,在计算机中我们或许更为喜欢用缓存。我个人认为是更为符合我们写出好用的代码片段的。
他的精髓所在正是将回调函数作用于数组中的各个成员上,而上一次return的值就作为memo,传入到下一个之中,你可以在这里面逐一处理:
不断更新迭代(累加)
将结果拼接(扁平化)
将特定的值添加到同一个对象之中(字符统计)
最后作为想要得到最终效果展现出来~
总结

写代码,大概是这么几个节点
了解语法
传参
返回
真正领悟到语言的特性,并能将其运用到实际开发中,写出更好更简洁更实用的代码
多看:要从从这些代码段中以及网上各路好汉写的实用代码学习怎么样真正地像写诗一样写代码~
多写
第四步的多看,我感觉真的是收益匪浅,如果你有看源码的学习习惯[5],真的会学到很多~ 例如其实 Redux.compose中也用到了reduce。真的感觉有些代码,如果我从没有见到过,我绝对写不出来...
关于本文
来自:前舟
https://juejin.cn/post/7102591881204203557
The End
边栏推荐
- Unity technology - 2D project experience
- Curriculum Learning and Graph Neural Networks (or Graph Structure Learning)
- 剑指 Offer II 014. 字符串中的变位词
- 剑指 Offer II 015. 字符串中的所有变位词
- 图片批量下载 +图片马赛克:多张图片组成端午安康!
- Sword finger offer II 020 Number of palindrome substrings
- Intranet penetration Chapter 4
- CocosCreator旧活新整-合成大粽子
- 电脑系统怎么修改图片格式
- 正则表达式不含某字符串
猜你喜欢

Alleviate and repair Android studio Caton, kotlin code prompt is slow

Maui + MVVM + Siemens cross platform application practice

梯度下降引发AI大牛们“激辩”,网友:每个人的答案都值得一看

Internal network infiltration tunnel

缓解修复Android Studio卡顿,Kotlin代码提示慢

Reprint the Alibaba open source project egg JS technical documents cause "copyright disputes". How to use the loose MIT license?

剑指 Offer II 010. 和为 k 的子数组

内网渗透隧道

防火门监控系统在某住宅项目上的应用

Sword finger offer II 018 Valid palindrome
随机推荐
Reprint the Alibaba open source project egg JS technical documents cause "copyright disputes". How to use the loose MIT license?
【Multisim仿真】差分比例放大电路
JVM——》类文件
JVM -- class compilation process
项目成功了,最大的功臣居然是项目经理?
MySQL - isolation level of transactions
从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!
Application of fire emergency evacuation indication system in a biopharmaceutical factory project
Chapter 6 domain controller security
加密机与数据库加密产品的区别?
Sword finger offer II 012 The sum of left and right subarrays is equal
Never far away, five programming languages that may be doomed to decline
Sword finger offer II 021 Delete the penultimate node of the linked list
图片批量下载 +图片马赛克:多张图片组成端午安康
Tensorflow new document publishing: add CLP, dtensor State of the art models are ready
Fpga-vga display
上位机开发——Modbus到底有多快
shell xxx.sh: line 284: return: -1: invalid option
Sword finger offer II 018 Valid palindrome