loadsh
Lodash 是一个一致性、模块化、高性能的 JavaScript 实用工具库。
let _ = require('lodash');
Lodash 中文文档 https://www.lodashjs.com/
# 1.数组
_.chunk(array, [size=1]) //1【平分数组】返回新数组。 _.chunk(['a', 'b', 'c', 'd'], 3); // => [['a', 'b', 'c'], ['d']]
_.fill(array, value, [start=0], [end=array.length]) //6【填充数组】
_.reverse(array) //20【反转数组】
//1.1 过滤
_.compact(array) //2【滤掉假值】返回新数组。 _.compact([0, 1, false, 2, '', 3]); // => [1, 2, 3]
_.difference(array, [values]) //4【过滤元素】返回过滤后的新数组。 _.difference([3, 2, 1], [4, 2]); <span class="grey">// => [3, 1]
_.differenceBy(array, [values], [iteratee]) //4【过滤元素】返回过滤后的新数组。 迭代器[iteratee] (Array|Function|Object|string) 。
_.differenceWith(array, [values], [comparator]) //4【过滤元素】返回过滤后的新数组。 //比较器[comparator] (Function)。
_.without(array, [values]) //33【过滤数组】 _.without([2, 1, 2, 3], 1, 2); // => [3]
//1.2 删除
_.drop(array, [n=1]) //5【删除头部N个元素】去除array前面的n个元素,返回新数组。
_.dropRight(array, [n=1]) //5【删除尾部N个元素】 去除array后面的n个元素,返回新数组。
_.dropRightWhile(array, [predicate=_.identity]) //5【删除元素】去除array中从 predicate 返回假值开始到尾部的部分。predicate 会传入3个参数: (value, index, array)。
_.dropWhile(array, [predicate=_.identity]) //5【删除元素】 去除array中从起点开始到 predicate 返回假值结束部分。predicate 会传入3个参数: (value, index, array)。
_.initial(array) //12【去除最后一个元素】 返回新数组。
_.pull(array, [values]) //18 【移除元素】移除数组array中所有和给定值相等的元素,使用 SameValueZero 进行全等比较。 _.pull( [1, 2, 3, 1, 2, 3], 2, 3); // => [1, 1]
_.pullAll(array, values) //18【移除元素】参数为数组
_.pullAllBy(array, values, [iteratee=_.identity]) //18【移除元素】参数为数组,迭代器。
_.pullAllWith(array, values, [comparator]) //18【移除元素】参数为数组,比较器。
_.pullAt(array, [indexes]) //18【移除元素】根据索引 indexes,移除array中对应的元素,并返回被移除元素的数组。会改变原数组。
_.remove(array, [predicate=_.identity]) //19【移除元素】根据条件移除元素,并返回被移除元素的数组,会改变原数组。
//1.3 裁剪、提取
_.slice(array, [start=0], [end=array.length]) //21 【裁剪数组】返回 数组array 裁剪部分的新数组。
_.tail(array) //26 获取除了array数组第一个元素以外的全部元素。
_.take(array, [n=1]) //27【裁剪数组】 创建一个数组切片,从array数组的起始元素开始提取n个元素
_.takeRight(array, [n=1]) //27【裁剪数组】 从右往左
_.takeRightWhile(array, [predicate=_.identity]) //28【提取元素】 从array数组的最后一个元素开始提取元素,直到 predicate 返回假值
_.takeWhile(array, [predicate=_.identity]) //28【提取元素】直到 predicate 返回假值
//1.4 查询索引
_.indexOf(array, value, [fromIndex=0]) //11【获取元素首次索引】 返回首次 value 在数组array中被找到的 索引值
_.lastIndexOf(array, value, [fromIndex=array.length-1]) //16【获取元素首次索引】从右往左匹配
_.findIndex(array, [predicate=_.identity], [fromIndex=0]) //7【查询第一个符合条件的索引】 返回第一个通过 predicate 判断为真值的元素的索引值
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1]) //7 类似findIndex,从右到左查询
_.sortedIndex(array, value) //22 使用二进制的方式检索来决定 value值 应该插入到数组中 尽可能小的索引位置,以保证array的排序。 _.sortedIndex([30, 50], 40); // => 1
_.sortedIndexBy(array, value, [iteratee=_.identity]) //22 类似sortedIndex
_.sortedLastIndex(array, value) //23 类似sortedIndex,返回 value值 在 array 中尽可能大的索引位置
_.sortedLastIndexBy(array, value, [iteratee=_.identity]) //23 类似sortedLastIndex
_.sortedLastIndexOf(array, value) //24 类似 _.lastIndexOf,除了它是在已经排序的数组array上执行二进制检索。
//1.5 获取元素
_.head(array) //8【获取首个元素】 别名 _.first
_.last(array) //15【获取最后个元素】
_.nth(array, [n=0]) //17【获取第n个元素】如果n为负数,则返回从数组结尾开始的第n个元素。
//1.6 数组扁平化
_.flatten(array) //9【减少一级嵌套】 返回新数组。
_.flattenDeep(array) //9【递归为一维数组】
_.flattenDepth(array, [depth=1]) //9 根据 depth 递归减少 array 的嵌套层级
//1.7 合并、交集、并集、差集、去重
_.concat(array, [values]) //3【合并数组】返回新数组。 _.concat([1], 2, [3], [[4]]); // => [1, 2, 3, [4]]
_.intersection([arrays]) //13【获取多个数组交集】
_.intersectionBy([arrays], [iteratee=_.identity]) //13 类似intersection,iteratee迭代器处理。
_.intersectionWith([arrays], [comparator]) //13 类似intersection,comparator比较器。
_.join(array, [separator=',']) //14【拼接数组为字符串】
_.union([arrays]) //29【数组并集】 _.union([2], [1, 2]); // => [2, 1]
_.unionBy([arrays], [iteratee=_.identity]) //类似union,+迭代器。
_.unionWith([arrays], [comparator]) //类似 _.union,+比较器
_.xor([arrays]) //34 【数组差集】 _.xor([2, 1], [2, 3]); // => [1, 3]
_.xorBy([arrays], [iteratee=_.identity]) //【数组差集】+迭代器
_.xorWith([arrays], [comparator]) //【数组差集】+比较器
//1.8 去重
_.uniq(array) //30【单数组去重】
_.uniqBy(array, [iteratee=_.identity]) //【数组去重】+迭代器
_.uniqWith(array, [comparator]) //【数组去重】+比较器
_.sortedUniq(array) //25【数组去重】类似 _.uniq,除了它会优化排序数组
_.sortedUniqBy(array, [iteratee]) //25【数组去重】类似 _.uniqBy,+迭代器。
//1.9 重组,分组
_.fromPairs(pairs) //10 返回一个由键值对pairs构成的对象。 _.fromPairs([['fred', 30], ['barney', 40]]); // => { 'fred': 30, 'barney': 40 }
_.zip([arrays]) //31 【创建分组数组】 _.zip(['fred', 'barney'], [30, 40], [true, false]); // => [['fred', 30, true], ['barney', 40, false]]
_.zipObject([props=[]], [values=[]]) //类似 _.fromPairs,除了它接受2个数组,第一个数组中的值作为属性标识符(属性名),第二个数组中的值作为相应的属性值 //_.zipObject(['a', 'b'], [1, 2]);// => { 'a': 1, 'b': 2 }
_.zipObjectDeep([props=[]], [values=[]]) //类似 _.zipObject,除了它支持属性路径。 _.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]); // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
_.zipWith([arrays], [iteratee=_.identity]) //类似于_.zip,+迭代器
_.unzip(array) //32 【重组分组数组】 _.unzip([['fred', 30, true], ['barney', 40, false]]); // => [['fred', 'barney'], [30, 40], [true, false]]
_.unzipWith(array, [iteratee=_.identity]) //【重组分组数组】类似于_.unzip +迭代器 _.unzipWith( [[1, 10, 100], [2, 20, 200]], _.add); // => [3, 30, 300]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 2.集合(数组或对象)
//2.1 创建组成对象
_.countBy(collection, [iteratee=_.identity]) //创建一个组成对象,key是元素经过迭代函数处理后返回的结果,value是结果的数量。 _.countBy([6.1, 4.2, 6.3], Math.floor); // => { '4': 1, '6': 2 }
_.groupBy(collection, [iteratee=_.identity]) //创建一个组成对象,key是元素经过迭代函数处理后返回的结果,value是元素组成的数组。 _.groupBy([6.1, 4.2, 6.3], Math.floor); // => { '4': [4.2], '6': [6.1, 6.3] }
_.keyBy(collection, [iteratee=_.identity]) //创建一个组成对象, key是元素经过迭代函数处理后返回的结果,value是元素。 _.keyBy([{'dir':'left'},{'dir':'right'}],'dir'); // => {'left':{'dir':'left'}, 'right':{'dir':'right'} }
//2.2 遍历集合
_.forEach(collection, [iteratee=_.identity]) //【遍历集合】别名_.each,返回集合本身。
_.forEachRight(collection, [iteratee=_.identity]) //【遍历集合】从右往左,_.eachRight
_.map(collection, [iteratee=_.identity]) //【遍历集合】
_.filter(collection, [predicate=_.identity]) //【过滤集合】
_.find(collection, [predicate=_.identity], [fromIndex=0]) //【查找第一个符合条件的元素】
_.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1]) //类似_.find 从右往左匹配
_.flatMap(collection, [iteratee=_.identity]) //根据条件返回一个新扁平化的数组
_.flatMapDeep(collection, [iteratee=_.identity]) //类似 _.flatMap 不同之处在于,_.flatMapDeep 会继续扁平化递归映射的结果。
_.flatMapDepth(collection, [iteratee=_.identity], [depth=1]) //类似_.flatMap,不同之处在于,_.flatMapDepth 会根据指定的 depth(递归深度)继续扁平化递归映射结果。
_.partition(collection, [predicate=_.identity]) //【拆分数组】拆分为两组数组,第一组包含满足条件的,第二组包含不符合条件的。
_.reduce(collection, [iteratee=_.identity], [accumulator]) //压缩集合为一个值
_.reduceRight(collection, [iteratee=_.identity], [accumulator]) //从右到左压缩
_.reject(collection, [predicate=_.identity]) //【过滤集合】_.filter的反方法
//2.3 集合判断
_.some(collection, [predicate=_.identity]) //判断集合的元素是否存在符合条件的元素。
_.every(collection, [predicate=_.identity]) //判断集合的元素是否全部符合条件。
_.includes(collection, value, [fromIndex=0]) //判断集合是否包含某个值
//2.4 集合排序
_.orderBy(collection, [iteratees=[_.identity]], [orders]) //【数组排序】,返回新数组。 // 以 `user` 升序排序 再 `age` 以降序排序。 _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
_.sortBy(collection, [iteratees=[_.identity]]) //【数组排序】,返回新数组。 以 iteratee 处理的结果升序排序。
//2.5 集合随机
_.sample(collection) //从集合中获得一个随机元素
_.sampleSize(collection, [n=1]) //从集合中获得n个随机元素组成的数组。
_.shuffle(collection) //【打乱集合】
//2.6 其他
_.invokeMap(collection, path, [args]) //调用path(路径)上的方法处理集合中的每个元素,返回一个数组,包含每次调用方法得到的结果 //_.invokeMap([123, 456], String.prototype.split, ''); // => [['1', '2', '3'], ['4', '5', '6']]
_.size(collection) //返回集合的长度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 3.日期
_.now() //返回当前时间的时间戳
1
# 4.函数
_.after(n, func) //创建一个函数,当他被调用n或更多次之后将马上触发func
_.ary(func, [n=func.length]) //创建一个调用func的函数。调用func时最多接受 n个参数,忽略多出的参数。
_.unary(func) //创建一个最多接受一个参数的函数,忽略多余的参数。 _.map(['6', '8', '10'], _.unary(parseInt)); // => [6, 8, 10]
_.before(n, func) //创建一个调用func的函数,通过this绑定和创建函数的参数调用func,调用次数不超过 n 次。 之后再调用这个函数,将返回一次最后调用func的结果。
_.bind(func, thisArg, [partials]) //创建一个调用func的函数,thisArg绑定func函数中的 this (注:this的上下文为thisArg) ,并且func函数会接收partials附加参数。
_.curry(func, [arity=func.length]) //【柯里化函数】
_.curryRight(func, [arity=func.length]) //
_.debounce(func, [wait=0], [options={}]) //【防抖函数】
_.throttle(func, [wait=0], [options={}]) //【节流函数】
_.defer(func, [args]) //【推迟函数】推迟调用func,直到当前堆栈清理完毕。 调用时,任何附加的参数会传给func
_.delay(func, wait, [args]) //【延迟函数】延迟 wait 毫秒后调用 func。 调用时,任何附加的参数会传给func。
_.flip(func) //【翻转函数】翻转函数的参数,返回新函数。
_.memoize(func, [resolver]) //【缓存函数】
_.negate(predicate) //创建一个针对断言函数 func 结果取反的函数。
_.once(func) //【一次性函数】
_.overArgs(func, [transforms=[_.identity]]) //【包裹函数】创建一个函数,调用func时参数为相对应的transforms的返回值。
_.partial(func, [partials]) //预设参数的函数
_.rearg(func, indexes) //创建一个函数,调用func时,根据指定的 indexes 调整对应位置参数。
_.rest(func, [start=func.length-1]) //创建一个函数,调用func时,this绑定到创建的新函数,并且start之后的参数作为数组传入。
_.spread(func, [start=0]) //创建一个函数,调用func时,this绑定到创建的新函数,把参数作为数组传入,类似于Function#apply
_.wrap(value, [wrapper=identity]) //创建一个函数。提供的 value 包装在 wrapper 函数的第一个参数里。 任何附加的参数都提供给 wrapper 函数。 被调用时 this 绑定在创建的函数上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 5.Lang
//5.1 拷贝
_.clone(value) //【浅拷贝】
_.cloneDeep(value) //【深拷贝】
_.cloneDeepWith(value, [customizer]) //【深拷贝+递归】
_.cloneWith(value, [customizer]) //类似 _.clone,除了它接受一个 customizer 定制返回的克隆值
_.conformsTo(object, source) //通过调用断言source的属性与 object 的相应属性值,检查 object是否符合 source。 _.conformsTo({ 'a': 1, 'b': 2 }, { 'b': n=> n > 1; }); //true
//5.2 大小比较
_.eq(value, other) //判断两个值是否相等
_.gt(value, other) //判断 value是否大于 other。
_.gte(value, other) //判断 value是否大于或者等于 other。
_.lt(value, other) //判断 value是否小于other。
_.lte(value, other) //判断 value是否小于或者等于 other。
//5.3 判断类型
_.isArguments(value) //判断 arguments
_.isArray(value) //判断 数组
_.isArrayBuffer(value) //判断 ArrayBuffer
_.isArrayLike(value) //判断 类数组
_.isArrayLikeObject(value) //判断 类数组&对象
_.isBoolean(value) //判断 boolean
_.isBuffer(value) //判断 buffer
_.isDate(value) //判断 Date对象
_.isElement(value) //判断是否DOM元素
_.isEmpty(value) //判断是否为空
_.isEqual(value, other) //深比较判断是否相等
_.isEqualWith(value, other, [customizer]) //比较规则
_.isError(value) //判断是否为错误对象
_.isFinite(value) //判断是否是原始有限数值
_.isFunction(value) //判断是否Function对象
_.isInteger(value) //判断是否整数
_.isLength(value) //判断是否为有效的类数组长度
_.isMap(value) //判断是否为Map对象
_.isMatch(object, source) //深比较,确定object是否含有和source完全相等的属性值
_.isMatchWith(object, source, [customizer]) //+比较器
_.isNaN(value) //判断是否为NaN
_.isNative(value) //判断是否是原生函数
_.isNil(value) //判断是否是null或undefined
_.isNull(value) //判断是否为null
_.isNumber(value) //判断是否为数值
_.isObject(value) //判断是否为Object对象
_.isObjectLike(value) //判断是否是类对象。
_.isPlainObject(value) //判断是否为普通对象
_.isRegExp(value) //判断是否为正则对象
_.isSafeInteger(value) //判断是否为安全整数
_.isSet(value) //判断是否是Set对象
_.isString(value) //判断是否是字符串
_.isSymbol(value) //判断是否是Symbol
_.isTypedArray(value) //判断是否是TypedArray
_.isUndefined(value) //判断是否是undefined
_.isWeakMap(value) //判断是否是WeakMap
_.isWeakSet(value) //判断是否是WeakSet
//5.4 类型转换
_.castArray(value) //如果 value 不是数组, 那么【强制转为数组】。
_.toArray(value) //转换为数组
_.toFinite(value) //转换为有限数字
_.toInteger(value) //转换为整数
_.toLength(value) //转换为用作类数组对象的长度整数
_.toNumber(value) //转换为数字
_.toPlainObject(value) //转换为普通对象
_.toSafeInteger(value) //转换为安全整数
_.toString(value) //转换为字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 6.Math
//6.1 两数的加减乘除
_.add(augend, addend) //两数相加
_.subtract(minuend, subtrahend) //两数相减
_.multiply(multiplier, multiplicand) //两数相乘
_.divide(dividend, divisor) //两数相除
//6.2 根据精度取舍
_.ceil(number, [precision=0]) //根据精度向上舍入 _.ceil(6.004, 2); // => 6.01 _.ceil(6040, -2); // => 6100
_.floor(number, [precision=0]) //根据精度向下舍入
_.round(number, [precision=0]) //根据精度四舍五入
//6.3 数组计算
_.max(array) //计算数组最大值
_.maxBy(array, [iteratee=_.identity]) //取最大值+迭代器
_.min(array) //求数组最小值
_.minBy(array, [iteratee=_.identity]) //数组最小值+迭代器
_.mean(array) //数组平均值
_.meanBy(array, [iteratee=_.identity]) //数组平均值+迭代器
_.sum(array) //数组求和
_.sumBy(array, [iteratee=_.identity]) //数字求和+迭代器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 7.Number
_.clamp(number, [lower], upper) //返回限制在 lower 和 upper 之间的值。
_.inRange(number, [start=0], end) //检查 n 是否在 start 与 end 之间,但不包括 end
_.random([lower=0], [upper=1], [floating]) //产生一个包括lower与upper之间的数
1
2
3
2
3
# 8.Object
//8.1 创建新对象
_.assign(object, [sources]) //分配来源对象的可枚举属性到目标对象上。
_.assignIn(object, [sources]) //类似_.assign,会遍历并继承来源对象的属性。别名 _.extend
_.assignInWith(object, sources, [customizer]) //+处理器 别名_.extendWith。
_.assignWith(object, sources, [customizer]) //
_.merge(object, [sources]) //类似_.assign,递归合并。会改变对象 object。
_.mergeWith(object, sources, customizer) //递归合并+处理函数
_.create(prototype, [properties]) //创建一个继承 prototype 的对象。
_.invert(object) //创建一个object键值倒置后的对象。 _.invert({ 'a': 1, 'b': 2, 'c': 1 }); => { '1': 'c', '2': 'b' }
_.invertBy(object, [iteratee=_.identity]) //_.invertBy({ 'a': 1, 'b': 2, 'c': 1 }, val=>'gg'+val); => { 'gg1': ['a','c'], 'gg2': ['b'] }
//8.1.2 过滤对象。
_.defaults(object, [sources]) //分配默认属性 _.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); // => { 'a': 1, 'b': 2 }
_.defaultsDeep(object, [sources]) //递归分配默认属性 _.defaultsDeep({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } });// => { 'a': { 'b': 2, 'c': 3 } }
_.omit(object, [props]) //忽略属性,返回新对象。_.omit({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']);// => { 'b': '2' }
_.omitBy(object, [predicate=_.identity]) //忽略属性+迭代器,返回新对象。_.omitBy({ 'a': 1, 'b': '2', 'c': 3 }, _.isNumber); // => { 'b': '2' }
_.pick(object, [props]) //选中属性,返回新对象。与_.omit相反。
_.pickBy(object, [props]) //选中属性+迭代器,返回新对象。与_.omitBy相反。
//8.2 遍历对象
_.findKey(object, [predicate=_.identity]) //类似_.find,返回最先被 predicate 判断为真值的元素 key,而不是元素本身
_.findLastKey(object, [predicate=_.identity]) //类似_.findKey,反向。
_.forIn(object, [iteratee=_.identity]) //遍历对象的自身和继承的可枚举属性。
_.forInRight(object, [iteratee=_.identity]) //类似_.forIn,反向遍历
_.forOwn(object, [iteratee=_.identity]) //使用 iteratee 遍历自身的可枚举属性
_.forOwnRight(object, [iteratee=_.identity]) //反向遍历自身可枚举属性
_.mapKeys(object, [iteratee=_.identity]) //遍历对象,经过迭代器(value, key, object)后替换原key
_.mapValues(object, [iteratee=_.identity]) //遍历对象,经过迭代器(value, key, object)后替换原value
_.transform(object, [iteratee=_.identity], [accumulator]) //转换object对象为一个新的accumulator对象,结果来自iteratee处理自身可枚举的属性
//8.2.2 遍历对象属性或值,创建数组。
_.functions(object) //创建一个函数属性名称的数组,函数属性名称来自object对象自身可枚举属性。
_.functionsIn(object) //创建一个函数属性名称的数组,函数属性名称来自object对象自身和继承的可枚举属性
_.entries(object) //创建一个object对象自身可枚举属性的键值对数组。数组可以通过_.fromPairs撤回。别名_.toPairs。
_.entriesIn(object) //创建一个object对象自身和继承的可枚举属性的键值对数组。数组可通过_.fromPairs撤回。别名_.toPairsIn。
_.keys(object) //创建一个 object 的自身可枚举属性名为数组。
_.keysIn(object) //创建一个 object 自身 和 继承的可枚举属性名为数组。
_.values(object) //创建 object 自身可枚举属性的值为数组。
_.valuesIn(object) //创建 object 自身和继承的可枚举属性的值为数组
//8.3 路径相关
_.at(object, [paths]) //创建数组,值来自object 的paths路径相应的值
_.get(object, path, [defaultValue]) //根据 object对象的path路径获取值 _.get({ 'a': [{ 'b': { 'c': 3 } }] }, 'a[0].b.c'); // => 3
_.result(object, path, [defaultValue]) //类似_.get,除了如果解析到的值是一个函数的话,就绑定 this 到这个函数并返回执行后的结果。
_.has(object, path) //检查 path 是否是object对象的直接属性。
_.hasIn(object, path) //检查 path 是否是object对象的直接或继承属性。
_.invoke(object, path, [args]) //调用object对象path上的方法。[args]为调用的方法的参数。 [args] (...*): 调用的方法的参数。
_.set(object, path, value) //设置 object的 path路径上的值。会改变 object。
_.setWith(object, path, value, [customizer]) //类似_.set+处理函数。会改变 object。
_.unset(object, path) //移除object对象 path 路径上的属性,返回true。会改变源object
_.update(object, path, updater) //更新object的path路径上的值,返回该值。会改变源object
_.updateWith(object, path, updater, [customizer]) //类似_.update+处理器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 9.Seq
//9.1 lodash 链式调用。
_(value) //创建一个lodash对象,包装value后的对象启用隐式方法链。返回的数组、集合、方法相互之间能够链式调用。_#value 解除链
_.chain(value) //建一个lodash包装实例,包装value以启用显式链模式。_#value 解除链。
_.tap(value, interceptor) //调用一个 interceptor 并返回 value。调用拦截器修改中间结果。
_.thru(value, interceptor) //类似_.tap, 除了它返回 interceptor 的返回结果。tap的拦截函数不用return,thru的需要。
//9.2 lodash原型链
_.prototype[Symbol.iterator] //启用包装对象为 iterable,启用包装对象为 iterable。
_.prototype.at([paths]) //这个方法是_.at 的包装版本。
_.prototype.chain() //创建一个lodash包装实例,启用显式链模式。
_.prototype.commit() //执行链式队列并返回结果。
_.prototype.next() //获得包装对象的下一个值,遵循iterator protocol。
_.prototype.plant(value) //创建一个链式队列的拷贝,传入的 value 作为链式队列的值。
_.prototype.reverse() //这个方法是_.reverse 的包装版本
_.prototype.value() //执行链式队列并提取解链后的值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 10.String
//10.1 转换字符串
_.camelCase([string='']) //转换字符串string为小驼峰写法
_.kebabCase([string='']) //转换字符串string为kebab case. _.kebabCase('fooBar'); // => 'foo-bar'
_.lowerCase([string='']) //转换字符串string以空格分开单词,并转换为小写。 _.kebabCase('fooBar'); // => 'foo bar'
_.upperCase([string='']) //转换字符串string为 空格 分隔的大写单词。 _.kebabCase('fooBar'); // => 'FOO BAR'
_.snakeCase([string='']) //转换字符串string为snake case. _.snakeCase('Foo Bar');// => 'foo_bar'
_.startCase([string='']) //转换 string 字符串为start case。 _.startCase('fooBar'); // => 'Foo Bar'
//10.1.2 大小写转换
_.capitalize([string='']) //转换字符串string首字母为大写,剩下为小写。_.capitalize('fred'); // => 'Fred'
_.lowerFirst([string='']) //转换字符串string的首字母为小写。 _.upperFirst('fred'); // => 'Fred'
_.upperFirst([string='']) //转换字符串string的首字母为大写。
_.toLower([string='']) //转换小写
_.toUpper([string='']) //转换大写
//10.1.3 转义
_.escape([string='']) //转义字符串为HTML实体字符。
_.escapeRegExp([string='']) //转义 RegExp 字符串中特殊的字符 "^", "$", "", ".", "*", "+", "?", "(", ")", "[", "]", ", ", 和 "|" in .
_.unescape([string='']) //_.escape的反向版。 这个方法转换string字符串中的 HTML 实体 &, <, >, ", ', 和 ` 为对应的字符。
//10.2 填充与去空
_.pad([string=''], [length=0], [chars=' ']) //字符串长度小于length则左右侧填充
_.padEnd([string=''], [length=0], [chars=' ']) //字符串长度小于length则左侧填充
_.padStart([string=''], [length=0], [chars=' ']) //字符串长度小于length则右侧填充
_.trim([string=''], [chars=whitespace]) //从string字符串中移除前面和后面的 空格 或 指定的字符。
_.trimEnd([string=''], [chars=whitespace]) //从string字符串中移除后面的 空格 或 指定的字符。
_.trimStart([string=''], [chars=whitespace]) //从string字符串中移除前面的 空格 或 指定的字符。
//10.3 字符串操作
_.parseInt(string, [radix=10]) //转换string字符串为指定基数的整数。
_.repeat([string=''], [n=1]) //重复字符串
_.replace([string=''], pattern, replacement) //替换string字符串中匹配的pattern为给定的replacement 。
_.split([string=''], separator, [limit]) //根据separator 拆分字符串string,限制数量。
_.words([string=''], [pattern]) //拆分字符串string中的词为数组 。_.words('fred, barney, & pebbles', /[^, ]+/g);// => ['fred', 'barney', '&', 'pebbles']
_.truncate([string=''], [options=]) //截断string字符串,如果字符串超出了限定的最大值。
//10.4 检查字符串
_.endsWith([string=''], [target], [position=string.length]) //检查字符串string是否以给定的target字符串结尾
_.startsWith([string=''], [target], [position=0]) //检查字符串string是否以 target 开头。
_.template([string=''], [options=]) //创建一个预编译模板方法,可以插入数据到模板中 "interpolate" 分隔符相应的位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 11.Util
_.attempt(func, [args]) //尝试调用func,返回结果 或者 捕捉错误对象。任何附加的参数都会在调用时传给func。
_.bindAll(object, methodNames) //绑定一个对象的方法到对象本身,覆盖现有的方法。
//11.1 创建函数
_.cond(pairs) //创建了一个函数,这个函数会迭代pairs,并调用最先返回真值对应的函数。该断言函数对绑定 this 及传入创建函数的参数。
_.conforms(source) //创建一个函数。 这个函数会 调用 source 的属性名对应的 predicate 与传入对象相对应属性名的值进行断言处理。 如果都符合返回 true ,否则返回 false 。
_.iteratee([func=_.identity]) //创建一个函数,通过创建函数的参数调用 func 函数。
_.matches(source) //创建一个深比较的方法来比较给定的对象和 source 对象。
_.matchesProperty(path, srcValue) //创建一个深比较的方法来比较给定对象的 path 的值是否是 srcValue
_.method(path, [args]) //创建一个调用给定对象 path 上的函数。 任何附加的参数都会传入这个调用函数中。
_.methodOf(object, [args]) //_.method的反向版。 这个创建一个函数调用给定 object 的 path 上的方法, 任何附加的参数都会传入这个调用函数中。
_.mixin([object=lodash], source, [options=]) //添加来源对象自身的所有可枚举函数属性到目标对象。 如果 object 是个函数,那么函数方法将被添加到原型链上。
_.nthArg([n=0]) //创建一个函数,这个函数返回第 n 个参数。如果 n为负数,则返回从结尾开始的第n个参数。
_.over([iteratees=[_.identity]]) //创建一个函数,传入提供的参数的函数并调用 iteratees 返回结果。
_.overEvery([predicates=[_.identity]]) //建一个函数,传入提供的参数的函数并调用 predicates 判断是否 全部 都为真值。
_.overSome([predicates=[_.identity]]) //创建一个函数,传入提供的参数的函数并调用 predicates 判断是否 存在 有真值。
_.property(path) //创建一个返回给定对象的 path 的值的函数。
_.propertyOf(object) //_.property的反相版本。 这个方法创建的函数返回给定 path 在object上的值。
_.runInContext([context=root]) //创建一个给定context上下文对象的原始的 lodash 函数。
//11.1.2 组合函数
_.flow([funcs]) //函数组合:连续调用,传入的参数都是前一个函数返回的结果。
_.flowRight([funcs]) //函数组合:从右到左依次调用
//11.2 返回固定值
_.defaultTo(value, defaultValue) //检查value,以确定一个默认值是否应被返回。如果value为NaN, null, 或者 undefined,那么返回defaultValue默认值。
_.identity(value) //这个方法返回首个提供的参数。
_.constant(value) //创建一个返回 value 的函数
_.noop() //这个方法返回 undefined。
_.stubArray() //返回空数组 []
_.stubFalse() //返回false
_.stubObject() //返回空对象 {}
_.stubString() //返回空字符串 ''
_.stubTrue() //返回true
//11.4 其他
_.noConflict() //释放 _ 变量为原来的值,并返回一个 lodash 的引用。
_.range([start=0], end, [step=1]) //创建一个包含从 start 到 end,但不包含 end 本身范围数字的数组。
_.rangeRight([start=0], end, [step=1]) //类似_.range , 除了它是降序生成值的
_.times(n, [iteratee=_.identity]) //调用 iteratee n 次,每次调用返回的结果存入到数组中。
_.toPath(value) //转化 value 为属性路径的数组。.toPath('a[0].b.c');// => ['a', '0', 'b', 'c']
_.uniqueId([prefix='']) //生成唯一ID。 如果提供了 prefix ,会被添加到ID前缀上。_.uniqueId('contact_');// => 'contact_104'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
上次更新: 2021/01/06, 14:01:00