lodash常见的方法
- 电脑硬件
- 2025-07-21 19:22:34

debounce 防抖
延迟 wait 毫秒后调用 func 方法。 提供 cancel 方法取消延迟的函数调用和 flush 方法立即调用。 可以提供一个 options(选项){leading ,trailing} 决定延迟前后如何触发(注:是 先调用后等待 还是 先等待后调用)。 func 调用时会传入最后一次提供给 debounced(防抖动)函数的参数。 后续调用的 debounced(防抖动)函数返回是最后一次 func 调用的结果。
_.debounce(func, [wait=0], [options=]) func (Function): 要防抖动的函数。 [wait=0] (number): 需要延迟的毫秒数。 [options=] (Object): 选项对象。 [options.leading = false] (boolean): 指定在延迟开始前调用。 [options.maxWait] (number): 设置 func 允许被延迟的最大值。 [options.trailing = true] (boolean): 指定在延迟结束后调用。
注意: 如果 leading 和 trailing 选项为 true, 则 func 允许 trailing 方式调用的条件为: 在 wait 期间多次调用防抖方法。 如果 wait 为 0 并且 leading 为 false, func调用将被推迟到下一个点,类似setTimeout为0的超时。
const fn = debounce(async ()=>{},500) var fn = debounce(()=>{}, 250, { maxWait: 1000 }); // 避免窗口在变动时出现昂贵的计算开销。 jQuery(window).on('resize', _.debounce(calculateLayout, 150)); // 当点击时 `sendMail` 随后就被调用。 jQuery(element).on('click', _.debounce(sendMail, 300, { 'leading': true, 'trailing': false })); // 确保 `batchLog` 调用1次之后,1秒内会被触发。 var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); var source = new EventSource('/stream'); jQuery(source).on('message', debounced); // 取消一个 trailing 的防抖动调用 jQuery(window).on('popstate', debounced.cancel); throttle创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。 该函数提供一个 cancel 方法取消延迟的函数调用以及 flush 方法立即调用。 可以提供一个 options 对象决定如何调用 func 方法, options.leading 与|或 options.trailing 决定 wait 前后如何触发。 func 会传入最后一次传入的参数给这个函数。 随后调用的函数返回是最后一次 func 调用的结果。
_.throttle(func, [wait=0], [options=]) func (Function): 要节流的函数。 [wait=0] (number): 需要节流的毫秒。 [options=] (Object): 选项对象。 [options.leading=true] (boolean): 指定调用在节流开始前。 [options.trailing=true] (boolean): 指定调用在节流结束后。
注意: 如果 leading 和 trailing 都设定为 true 则 func 允许 trailing 方式调用的条件为: 在 wait 期间多次调用。 如果 wait 为 0 并且 leading 为 false, func调用将被推迟到下一个点,类似setTimeout为0的超时。
// 避免在滚动时过分的更新定位 jQuery(window).on('scroll', _.throttle(updatePosition, 100)); // 点击后就调用 `renewToken`,但5分钟内超过1次。 var throttled = _.throttle(renewToken, 300000, { 'trailing': false }); jQuery(element).on('click', throttled); // 取消一个 trailing 的节流调用。 jQuery(window).on('popstate', throttled.cancel); delay延时函数
delay(function(text) { console.log(text); // => 一秒后输出 'later'。 }, 1000, 'later'); 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 }] escape escape('aa, bb, &<>" cc');//aa, bb, &<>" cc 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> at var object = { 'a': [ { 'b': { 'c': 3 } }, 4 ] }; at(object, ['a[0].b.c', 'a[1]']) // => [ 3, 4 ] before const fn = before(3, () => { console.log(11111); }) fn() // 11111 fn() // 11111 fn() // 没有反应 fn() // 没有反应 after var fn = after(3, function () { console.log('1111'); }); fn(); // 无反应 fn(); // 无反应 fn(); // 1111 fn(); // 1111 fn(); // 1111 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 } chunk chunk(['a', 'b', 'c', 'd', 'e'], 2) // [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e' ] ] clone var a = [{ 'a': 1 }, { 'b': 2 }]; var b = clone(a); b[0] === a[0] // true cloneDeep var a = [{ 'a': 1 }, { 'b': 2 }]; var b = cloneDeep(a); b[0] === a[0] // false 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 isEqual isEqual({}, {}) // true 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 }] 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 }] 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 }] 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 }] 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] isMatch var object = { a: 1, b: 2, c: 3 }; isMatch(object, { b: 2, c: 3 }) // true isMatch(object, { b: 1 }) // false pick var object = { 'a': 1, 'b': '2', 'c': 3 }; pick(object, ['a', 'c']);// => { 'a': 1, 'c': 3 } pickBy(object,isNumber); // => { 'a': 1, 'c': 3 } 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]] sample sample([1, 2, 3, 4]); // => 随机得到一项 sampleSize([1, 2, 3], 2);// => [3, 1] sampleSize([1, 2, 3], 4);// => [2, 3, 1] shuffle // 打乱数组 shuffle([1, 2, 3, 4]);// => [4, 1, 3, 2] 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'] } uniqueId uniqueId('contact_');// => 'contact_104' uniqueId();// => '105'lodash常见的方法由讯客互联电脑硬件栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“lodash常见的方法”