lodash常用方法

3,414 阅读2分钟

1 深层查找属性值 放入一个数组

    var ownerArr = [{    "owner": "Colin",    "pets": [{        "name": "dog1"    }, {        "name": "dog2"    }]
    }, {
    "owner": "John",
    "pets": [{
        "name": "dog3"
    }, {
        "name": "dog4"
    }]
    }];
    console.log(_.map(ownerArr, "pets[0].name"));
    // => ['dog1','dog3']

2 深拷贝

    var objB = _.cloneDeep(objA);
    objB === objA // false

3 随机数

    _.random(15, 20);   //15到20的整数
    _.random(15, 20, true);   //15到20的小数

4 对象去除属性

    var objA = {"name": "colin", "car": "suzuki", "age": 17};

    objA = _.omit(objA, ['car', 'age']);
    // => {"name": "colin"}

    objA = _.omit(objA, _.isNumber);
    // => {"name": "colin", "car": "suzuki"};
  • 对象留下属性 用法与omit基本一致 _.pick(object, [props])

5 数组拆分

    const arr = [1,2,3,4,5,6,7,8,9];
    _.chunk(arr,2)    //每两个拆分为一个新的数组   
    // =>[[1,2],[3,4],[5,6],[7,8],[9]]

6 数组去除假值 已有 Array.filter(Boolean)代替

    _.compact(['1','2',' ',0])
    // => ['1','2']

7 数组排除含有指定值

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

8 数组去除前n个元素,默认为一个 _.dropRight为去除后面

    _.drop([1, 2, 3],n);
    // => [2, 3]

9 数组返回新的对象

    _.fromPairs([['fred', 30], ['barney', 40]]);
    // => { 'fred': 30, 'barney': 40 }

10 数组交集

    _.intersection([2, 1], [4, 2], [1, 2]);
    // => [2]

11 数组递归为一组

    _.flattenDeep([1, [2, [3, [4]], 5]]);
    // => [1, 2, 3, 4, 5]

12 数组合并为对象

    _.zipObject(['a', 'b'], [1, 2]);
    // => { 'a': 1, 'b': 2 }

13 数组强制转换

    _.castArray(1);
    // => [1]
    
    _.castArray({ 'a': 1 });
    // => [{ 'a': 1 }]

14 延迟 wait 毫秒后调用 func。 调用时,任何附加的参数会传给func。

    _.delay(function (text) {
        console.log(text);
    }, 1000, 'later');

15 只调用一次的函数

    var initialize = _.once(fn);
    initialize();
    initialize();
    // `initialize` 只能调用 `fn` 一次。

16 数组求和

    _.sum([4, 2, 8, 6]);
    // => 20

17 对象值求和

    var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];

    _.sumBy(objects, function(o) { return o.n; });
    // => 20
    
    _.sumBy(objects, 'n');
    // => 20

18 数字是否在指定区间 _.inRange(number, [start=0], end)

    _.inRange(3, 2, 4);
    // => true
    
    _.inRange(4, 8);
    // => true
    
    _.inRange(4, 2);
    // => false
    
    _.inRange(2, 2);
    // => false
    
    _.inRange(1.2, 2);
    // => true
    
    _.inRange(5.2, 4);
    // => false
    
    _.inRange(-3, -2, -6);
    // => true

19 对象取值放入数组 _.at(object, [paths])

    var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };

    _.at(object, ['a[0].b.c', 'a[1]']);
    // => [3, 4]

20 移除object对象 path 路径上的属性 _.unset(object, path)

// 返回布尔值  之前的对象也发生了改变
var object = { 'a': [{ 'b': { 'c': 7 } }] };
_.unset(object, 'a[0].b.c');
// => true

console.log(object);
// => { 'a': [{ 'b': {} }] };

_.unset(object, ['a', '0', 'b', 'c']);
// => true

console.log(object);
// => { 'a': [{ 'b': {} }] };

21 修改object对象 path 路径上的属性

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.update(object, 'a[0].b.c', function (n) { return n * n; });
console.log(object);
// => { 'a': [{ 'b': { 'c': 3 } }] }

22 判断对象是否含有指定属性

var users = [
  { 'user': 'barney', 'active': true },
  { 'user': 'fred',   'active': false }
];
 
// The `_.matches` iteratee shorthand.
_.some(users, { 'user': 'barney', 'active': false });
// => false

以上是个人对lodash常用总结,摘自 lodash 中文文档 ,了解更多请访问 lodash