当前位置:网站首页>JS小技巧,让你编码效率杠杠的,快乐摸鱼
JS小技巧,让你编码效率杠杠的,快乐摸鱼
2022-08-11 05:29:00 【wendZzz】
寻找最大值最小值,求和
reduce版
总和
var array = [1,2,3,4,5] array.reduce((a, b) => a + b)
示例:

最大值
var array = [12, 34, 22, 32, 21] array.reduce((a, b) => a > b ? a : b)
示例:

最小值
var array = [12, 34, 22, 32, 21] array.reduce((a, b) => a < b ? a : b)
示例:

排序
字符串排序
升序
var array = ["jack", "lucy", "bert", "tom", "beenle", "lirika"] array.sort()
示例:

降序
var array = ["jack", "lucy", "bert", "tom", "beenle", "lirika"] array.sort().reverse()
示例:

过滤数组中假值
var array = [1, "11", 0, "0", false, true, null, undefined, ''] array.filter(Boolean)
示例:

去重
数字去重
var array = [1,2,3,4,5,1,3,5,6] [...new Set(array)]
示例:

对象数组去重
/**
* arr: 作用数组
* uniKey: 去重字段
*/
function uniqueFunc(arr, uniKey){
const res = new Map();
return arr.filter((item) => !res.has(item[uniKey]) && res.set(item[uniKey], 1));
}示例:

边栏推荐
- Promise 中状态改变和回调执行先后顺序 和promise多次回调
- 2021-09-11 C语言 变量与内存分配
- C language implementation guess Numbers (with source code, can be directly run)
- Goldbach's conjecture and the ring of integers
- Matplotlib找不到字体,打印乱码
- JS案例练习(pink老师经典案例)
- Byte (byte) and bit (bit)
- Open Source Machine Learning Database OpenMLDB Contributor Program Fully Launched
- ARM 汇编指令 ADR 与 LDR 使用
- 字节(byte)和位(bit)
猜你喜欢
随机推荐
Pinyougou project combat notes
编译异常解决
使用adb命令管理应用
promise.all 学习(多个promise对象回调)
Tinker的自我介绍
scanf函数在混合接受数据(%d和%c相连接)时候的方式
Interpretation of the paper: GAN and detection network multi-task/SOD-MTGAN: Small Object Detection via Multi-Task Generative Adversarial Network
品优购项目实战笔记
Node stepping on the pit 80 port is occupied
Tinker's self-introduction
何凯明新作ViTDET:目标检测领域,颠覆分层backbone理念
vscode插件开发——代码提示、代码补全、代码分析
Day 86
js 学习进阶(Dom部分 pink老师教学笔记)
Use the adb command to manage applications
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
字节(byte)和位(bit)
Certificate of SearchGuard configuration
Day 79
Byte (byte) and bit (bit)








