环企首页
lodash
概述
lodash 是 JavaScript 的实用工具库,提供了大量对数组、对象、字符串、数字、函数、数据类型的操作函数。API 统一规范,分为 _.map、_.filter 等函数式风格和 _().map() 的链式风格两类。
安装:
npm install lodash
常用函数
数组
const _ = require('lodash')
_.chunk([1,2,3,4,5], 2) // [[1,2],[3,4],[5]] 分块
_.compact([0,1,false,2,'',3]) // [1,2,3] 去除假值
_.concat([1,2], [3], [[4]]) // [1,2,3,4] 合并
_.difference([2,1],[2,3]) // [1] 差集
_.drop([1,2,3], 2) // [3] 去掉前 N 个
_.dropRight([1,2,3], 1) // [1,2] 去掉末尾 N 个
_.fill([1,2,3], '*', 1, 2) // [1,'*','*'] 填充
_.flatten([1,[2,[3]]]) // [1,2,[3]] 展平一层
_.flattenDeep([1,[2,[3]]]) // [1,2,3] 深度展平
_.groupBy([6.1,4.2,6.3], Math.floor) // {4:[4.2],6:[6.1,6.3]}
_.intersection([[2,1],[2,3]]) // [2] 交集
_.uniq([2,1,2]) // [2,1] 去重
_.union([[2,1],[2,3]]) // [2,1,3] 并集
_.zip(['a','b'],[1,2]) // [['a',1],['b',2]] 打包
_.unzip([['a',1],['b',2]]) // [['a','b'],[1,2]] 解包
集合(map/filter/reduce)
_.map([1,2,3], n => n * 2) // [2,4,6]
_.filter([1,2,3,4], n => n % 2) // [1,3]
_.find([1,2,3,4], n => n > 2) // 3(第一个满足条件的)
_.findLast([1,2,3], n => n > 2) // 3(从右往左找)
_.reduce([1,2,3,4], (acc, n) => acc + n, 0) // 10
_.reduceRight([1,2,3], (acc, n) => [n,...acc], []) // [3,2,1]
_.some([1,2,6], n => n > 5) // true
_.every([1,2,6], n => n > 0) // true
_.includes([1,2,3], 2) // true(支持字符串)
_.size({a:1,b:2}) // 2
对象
_.get({a:{b:1}}, 'a.b') // 1(安全取值,路径不存在返回 undefined)
_.get({a:{b:1}}, 'a.c', 'default') // 'default'(带默认值)
_.set({a:{}}, 'a.b', 2) // {a:{b:2}}(安全设值)
_.unset(obj, 'a.b') // true(删除属性)
_.pick({a:1,b:2,c:3}, ['a','c']) // {a:1,c:3}
_.omit({a:1,b:2,c:3}, ['b']) // {a:1,c:3}
_.merge({a:1},{a:2,b:2}) // {a:2,b:2}(深度合并)
_.assign({a:1},{a:2,b:2}) // {a:2,b:2}(浅合并)
_.clone({a:1}) // 浅拷贝
_.cloneDeep({a:{b:1}}) // 深拷贝
_.keys({a:1,b:2}) // ['a','b']
_.values({a:1,b:2}) // [1,2]
_.entries({a:1,b:2}) // [['a',1],['b',2]]
_.fromEntries([['a',1]]) // {a:1}
_.has({a:{b:1}}, 'a.b') // true(检查路径是否存在)
字符串
_.camelCase('foo-bar') // 'fooBar'
_.kebabCase('fooBar') // 'foo-bar'
_.snakeCase('fooBar') // 'foo_bar'
_.startCase('foo-bar') // 'Foo Bar'
_.upperFirst('foo') // 'Foo'
_.lowerFirst('FOO') // 'fOO'
_.trim(' hello ') // 'hello'
_.truncate('hello world', { length: 5, omission: '...' }) // 'hello...'
_.padStart('5', 3, '0') // '005'
_.padEnd('5', 3, '0') // '500'
_.repeat('*', 3) // '***'
_.replace('hello world', 'world', 'js') // 'hello js'
_.split('a-b-c', '-') // ['a','b','c']
数字
_.random(1, 10) // 1-10 随机整数
_.random(1, 10, true) // 1-10 随机小数
_.clamp(-5, 0, 10) // 0(限制范围)
_.inRange(3, 1, 5) // true
_.sum([1,2,3]) // 6
_.mean([1,2,3]) // 2
_.round(4.006) // 4
_.ceil(4.006) // 5
_.floor(4.006) // 4
函数
_.debounce(func, 500) // 防抖(500ms 内只执行最后一次)
_.throttle(func, 1000) // 节流(每 1000ms 最多执行一次)
_.once(func) // 只执行一次
_.memoize(func) // 缓存结果
_.curry(func) // 柯里化
_.partial(func, arg1) // 偏函数(预设部分参数)
_.once(func) // 只执行一次
_.delay(func, 1000) // 延迟 1000ms 执行
_.noop() // 空函数
_.identity(5) // 返回参数本身
常用工具
_.isEmpty({}) // true
_.isNil(null) // true(null/undefined)
_.isArray([1,2]) // true
_.isString('abc') // true
_.isNumber(123) // true
_.isObject({}) // true
_.isFunction(fn) // true
_.isPlainObject({}) // true(普通对象,非数组/函数等)
_.range(5) // [0,1,2,3,4]
_.range(1, 5) // [1,2,3,4]
_.range(0, 30, 5) // [0,5,10,15,20,25]
_.uniqueId('user_') // 'user_1'(递增唯一 ID)
链式调用
_.chain([1,2,3,4,5])
.filter(n => n % 2 === 0)
.map(n => n * n)
.sum()
.value() // 40
常见问题
| 问题 | 解决方案 |
|---|---|
| 只想用部分函数 | 按需导入:const chunk = require('lodash/chunk') |
| 需要安全取值 | _.get(obj, 'a.b.c') 替代 obj.a.b.c |
| 大量 lodash 拖累体积 | 用 lodash-es + tree-shaking,或用原生方法替代 |
| 想移除空值 | _.compact(arr) 或 _.omitBy(obj, _.isNil) |