Lodash是什么
Lodash 是一个一致性、模块化、高性能的JavaScript 实用工具库。
为什么使用Lodash
Lodash能够降低array、number、object、string等的使用难度,从而使使用Javascript更加简单。
Lodash的模块化非常适用于:
- 遍历Array、Object、String
- 对值进行操作和检测
- 创建符合功能的函数
使用
安装
1 | npm install lodash |
使用
1 | const _ = require('lodash') |
常用函数
1. memoize 记忆函数
1 | //模拟memoize函数 |
2. curry 函数柯里化
1 | //模拟curry |
3. 组合函数
1 | //模拟flowRight |
ps: reduce()方法:
reduce()
方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。
reducer 函数接收四个参数
- Accumulator(acc)(累计器)
- Current Value(cur)(当前值)
- Current Index(idx)(当前索引)
- Source Array(src)(源数组)
语法:
1 | arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue]) |
initialValue:作为第一次调用 callback
函数时的第一个参数的值。
如果没有提供初始值,则将使用数组中的第一个元素。
在没有初始值的空数组上调用 reduce 将报错。