【总结】lodash.js 工具库

lodash是一套工具库,内部封装了很多字符串、数组、对象等常见数据类型的处理函数。

一、引入lodash.js

二、使用

1、切割 _.chunk(array, [size=1])

_.chunk(['a', 'b', 'c', 'd'], 2);

// => [['a', 'b'], ['c', 'd']]

_.chunk(['a', 'b', 'c', 'd'], 3);

// => [['a', 'b', 'c'], ['d']]

2、去假值_.compact(array)

创建一个新数组,包含原数组中所有的非假值元素。例如falsenull0""undefined, 和 NaN 都是被认为是“假值”。

_.compact([0, 1, false, 2, '', 3]);

// => [1, 2, 3]

3、将值和数组连接起来_.concat(array, [values])

创建一个新数组,将array与任何数组 或 值连接在一起。

var array = [1];

var other = _.concat(array, 2, [3], [[4]]);

console.log(other);

// => [1, 2, 3, [4]]

console.log(array);

// => [1]

4、排除_.difference(array, [values])(第二个数组是要被排除的内容)(返回新数组)

_.difference([3, 2, 1], [4, 2]);

// => [3, 1]

5、排除_.differenceBy(array, [values], [iteratee=_.identity])

_.differenceBy([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor);      Math.floor最小的整数

// => [3.1, 1.3]

// The `_.property` iteratee shorthand.

_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');

// => [{ 'x': 2 }]

6、_.differenceWith(array, [values], [comparator])(不懂)

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];

_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);

// => [{ 'x': 2, 'y': 1 }]

7、去除array前面的n个元素_.drop(array, [n=1])   默认n=1

_.drop([1, 2, 3]);

// => [2, 3]

_.drop([1, 2, 3], 2);

// => [3]

_.drop([1, 2, 3], 5);

// => []

_.drop([1, 2, 3], 0);

// => [1, 2, 3]

8、去尾部_.dropRight(array, [n=1])

_.dropRight([1, 2, 3]);

// => [1, 2]

_.dropRight([1, 2, 3], 2);

// => [1]

_.dropRight([1, 2, 3], 5);

// => []

_.dropRight([1, 2, 3], 0);

// => [1, 2, 3]

9、_.dropRightWhile(array, [predicate=_.identity])(不懂)

去除array中从 predicate 返回假值开始到尾部的部分。predicate 会传入3个参数: (value, index, array)

var users = [

  { 'user': 'barney',  'active': true },

  { 'user': 'fred',    'active': false },

  { 'user': 'pebbles', 'active': false }

];

_.dropRightWhile(users, function(o) { return !o.active; });

// => objects for ['barney']

// The `_.matches` iteratee shorthand.

_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });

// => objects for ['barney', 'fred']

// The `_.matchesProperty` iteratee shorthand.

_.dropRightWhile(users, ['active', false]);

// => objects for ['barney']

// The `_.property` iteratee shorthand.

_.dropRightWhile(users, 'active');

// => objects for ['barney', 'fred', 'pebbles']

10、_.dropWhile(array, [predicate=_.identity])(不懂)

var users = [

  { 'user': 'barney',  'active': false },

  { 'user': 'fred',    'active': false },

  { 'user': 'pebbles', 'active': true }

];

_.dropWhile(users, function(o) { return !o.active; });

// => objects for ['pebbles']

// The `_.matches` iteratee shorthand.

_.dropWhile(users, { 'user': 'barney', 'active': false });

// => objects for ['fred', 'pebbles']

// The `_.matchesProperty` iteratee shorthand.

_.dropWhile(users, ['active', false]);

// => objects for ['pebbles']

// The `_.property` iteratee shorthand.

_.dropWhile(users, 'active');

// => objects for ['barney', 'fred', 'pebbles']

11、_.includes(collection, value, [fromIndex=0])

检查是否valuecollection。如果collection是字符串,则检查其子字符串value

  1. collection (Array | Object | string):要检查的集合。
  2. value (*):要搜索的值。
  3. [fromIndex=0] (数字):要搜索的索引。

返回

(boolean)true如果value找到则返回,否则false

_.includes([1, 2, 3], 1);

// => true

_.includes([1, 2, 3], 1, 2);

// => false

_.includes({ 'a': 1, 'b': 2 }, 1);

// => true

_.includes('abcd', 'bc');

// => true

12、_.find(collection, [predicate=_.identity], [fromIndex=0])

迭代元素collection,返回第一个元素predicate

var users = [

  { 'user': 'barney',  'age': 36, 'active': true },

  { 'user': 'fred',    'age': 40, 'active': false },

  { 'user': 'pebbles', 'age': 1,  'active': true }

];

 

_.find(users, function(o) { return o.age < 40; });

// => object for 'barney'

 

// The `_.matches` iteratee shorthand.

_.find(users, { 'age': 1, 'active': true });

// => object for 'pebbles'

 

// The `_.matchesProperty` iteratee shorthand.

_.find(users, ['active', false]);

// => object for 'fred'

 

// The `_.property` iteratee shorthand.

_.find(users, 'active');

// => object for 'barney'

13、_.cloneDeep(value)

这种方法类似于_.clone递归克隆value。但是 _.clone克隆后和原先的===完全一样。

var objects = [{ 'a': 1 }, { 'b': 2 }];

 

var deep = _.cloneDeep(objects);

console.log(deep[0] === objects[0]);

// => false

14、_.findIndex(array, [predicate=_.identity], [fromIndex=0])

和find一样,但是返回索引

15、_.isEmpty(value)

检查是否value为空对象,集合,映射或集。

 

_.isEmpty(null);

// => true

 

_.isEmpty(true);

// => true

 

_.isEmpty(1);

// => true

 

_.isEmpty([1, 2, 3]);

// => false

 

_.isEmpty({ 'a': 1 });

// => false

16、_.isUndefined(value)

Checks if value is undefined.

 

_.isUndefined(void 0);

// => true

 

_.isUndefined(null);

// => false

16、_.map(collection, [iteratee=_.identity])

返回的结果是数组

使用一个三个参数调用iteratee:
(value,index | key,collection)。

function square(n) {

  return n * n;

}

 

_.map([4, 8], square);

// => [16, 64]

 

_.map({ 'a': 4, 'b': 8 }, square);

// => [16, 64] (iteration order is not guaranteed)

 

var users = [

  { 'user': 'barney' },

  { 'user': 'fred' }

];

 

// The `_.property` iteratee shorthand.

_.map(users, 'user');

// => ['barney', 'fred']

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值