lodash常见的方法

本文介绍了JavaScript中的防抖(debounce)和节流(throttle)技术,包括它们的实现原理、参数选项以及在实际场景中的应用,如DOM事件处理和长轮询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. debounce

    var fn = debounce(() => { }, 250, { maxWait: 500 })
    
  2. throttle

    var fn = throttle(() => { }, 250)
    
  3. delay

    delay(function(text) { 
      console.log(text); // => 一秒后输出 'later'。
    }, 1000, 'later');
    
  4. xor

    xor([1, 2], [2, 3], [3, 4]) // [1, 4]
    xorBy([{ x: 1 }], [{ x: 2 }, { x: 1, y: 1 }], 'x'); // [ { x: 2 } ]
    var objects = [{ x: 1, y: 2 }, { x: 2, y: 1 }];
    var others = [{ x: 1, y: 1 }, { x: 1, y: 2 }];
    xorWith(objects, others, _.isEqual); // => [{ x: 2, y: 1 }, { x: 1, y: 1 }]
    
  5. escape

    escape('aa, bb, &<>" cc');//aa, bb, &amp;&lt;&gt;&quot; cc
    
  6. wrap

    var p = wrap(escape, function (func, text) {
      return '<p>' + func(text) + '</p>';
    });
    p('aa, bb, & cc') // <p>fred%2C%20barney%2C%20%26%20pebbles</p>
    
  7. at

    var object = { 'a': [
                          { 'b': { 'c': 3 } }, 
                          4
                        ] 
                 };
    at(object, ['a[0].b.c', 'a[1]']) // => [ 3, 4 ]
    
  8. before

    const fn = before(3, () => {
      console.log(11111);
    })
    fn() // 11111
    fn() // 11111
    fn() // 没有反应
    fn() // 没有反应
    
  9. after

    var fn = after(3, function () {
      console.log('1111');
    });
    fn(); //  无反应 
    fn(); //  无反应 
    fn(); //  1111
    fn(); //  1111
    fn(); //  1111
    
  10. chain

    chain([
      { 'user': 'barney', 'age': 36 },
      { 'user': 'fred', 'age': 40 },
      { 'user': 'pebbles', 'age': 1 }
    ]).sortBy('age').map(function (o) {
      return o.user + ' is ' + o.age;
    }).head() // { 'user': 'pebbles', 'age': 1 }
    
  11. chunk

    chunk(['a', 'b', 'c', 'd', 'e'], 2) // [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e' ] ]
    
  12. clone

    var a = [{ 'a': 1 }, { 'b': 2 }];
    var b = clone(a);
    b[0] === a[0] // true
    
  13. cloneDeep

    var a = [{ 'a': 1 }, { 'b': 2 }];
    var b = cloneDeep(a);
    b[0] === a[0] // false
    
  14. isEmpty

    isEmpty(undefined);// => true
    isEmpty(null);// => true
    isEmpty(true);// => true
    isEmpty(1);// => true
    isEmpty("zanlan");// => true
    isEmpty([]);// => true
    isEmpty({});// => true
    isEmpty([1, 2, 3]);// => false
    isEmpty({ 'a': 1 });// => false
    
  15. isEqual

    isEqual({}, {}) // true
    
  16. intersection

    intersection([2, 1], [4, 2], [1, 2]); // => [2]
    
    intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor); // => [2.1]
    intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 1 }]
    
    var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
    var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
    intersectionWith(objects, others, isEqual);// => [{ 'x': 1, 'y': 2 }]
    
  17. uniq

    uniq([2, 1, 2]) // => [2, 1]
    
    uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
    // => [{ 'x': 1 }, { 'x': 2 }]
    
    var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]
    uniqWith(objects, isEqual);// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
    
  18. union

    union([2], [1, 2]) // => [2, 1]
    
    unionBy([2.1], [1.2, 2.3], Math.floor);// => [2.1, 1.2]
    
    unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');// => [{ 'x': 1 }, { 'x': 2 }]
    
    var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
    var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
    unionWith(objects, others, isEqual);
    // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
    
  19. difference

    difference([1, 2, 3], [1, 3, 4, 5]); // [2]
    
    differenceBy([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor);// => [3.1, 1.3]
    differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');// => [{ 'x': 2 }]
    
    var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
    differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);// => [{ 'x': 2, 'y': 1 }]
    
  20. flatten

    flatten([1, [2, [3, [4]], 5]]);// => [1, 2, [3, [4]], 5]
    
    flattenDeep([1, [2, [3, [4]], 5]]);// => [1, 2, 3, 4, 5]
    
    var array = [1, [2, [3, [4]], 5]];
    flattenDepth(array, 1); // => [1, 2, [3, [4]], 5]
    flattenDepth(array, 2); // => [1, 2, 3, [4], 5]
    
  21. isMatch

    var object = { a: 1, b: 2, c: 3 };
    isMatch(object, { b: 2, c: 3 }) // true
    isMatch(object, { b: 1 }) // false
    
  22. pick

    var object = { 'a': 1, 'b': '2', 'c': 3 };
    pick(object, ['a', 'c']);// => { 'a': 1, 'c': 3 }
    pickBy(object,isNumber); // => { 'a': 1, 'c': 3 }
    
  23. sortBy

    var users = [
      { 'user': 'fred',   'age': 48 },
      { 'user': 'barney', 'age': 36 },
      { 'user': 'fred',   'age': 40 },
      { 'user': 'barney', 'age': 34 }
    ];
    sortBy(users, function(o) { return o.user; });
    // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
     
    sortBy(users, ['user', 'age']);
    // => [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
     
    sortBy(users, 'user', function(o) {
      return Math.floor(o.age / 10);
    });
    // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
    
  24. sample

    sample([1, 2, 3, 4]); // => 随机得到一项
    sampleSize([1, 2, 3], 2);// => [3, 1]
    sampleSize([1, 2, 3], 4);// => [2, 3, 1]
    
  25. shuffle

    // 打乱数组
    shuffle([1, 2, 3, 4]);// => [4, 1, 3, 2]
    
  26. groupBy

    groupBy([6.1, 4.2, 6.3], Math.floor); // => { '4': [4.2], '6': [6.1, 6.3] }
    groupBy(['one', 'two', 'three'], 'length');// => { '3': ['one', 'two'], '5': ['three'] }
    
  27. uniqueId

    uniqueId('contact_');// => 'contact_104'
    uniqueId();// => '105'
    
  28. 2121

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值