当前位置:网站首页>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)); }
示例:
边栏推荐
- 使用adb命令管理应用
- Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
- Argparse模块 学习
- Interpretation of the paper: GAN and detection network multi-task/SOD-MTGAN: Small Object Detection via Multi-Task Generative Adversarial Network
- The third phase of the contributor task is wonderful
- Tinker的自我介绍
- Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB
- js learning advanced BOM part (pink teacher notes)
- 编译异常解决
- STM32学习笔记(白话文理解版)—按键控制
猜你喜欢
随机推荐
USB URB
SearchGuard配置
Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"
红外线一认识
Jetpack's dataBinding
STM32学习笔记(白话文理解版)—搞懂PWM输出
IIC and SPI
mysql基础总结
C语言实现简易扫雷(附带源码)
Vscode远程连接服务器终端zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
Byte (byte) and bit (bit)
Use the adb command to manage applications
Day 77
Argparse模块 学习
栈stack
OpenMLDB + Jupyter Notebook: Quickly Build Machine Learning Applications
Day 75
Intelligent risk control China design and fall to the ground
STM32-库函数-SetSysClock(void)函数解析-正点原子探索者
JS进阶网页特效(pink老师笔记)