当前位置: 首页 > article >正文

lodash常见的方法

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, &amp;&lt;&gt;&quot; 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'

http://www.kler.cn/a/152171.html

相关文章:

  • Zookeeper+Kafka集群
  • 玻色量子事件活动
  • XIAO ESP32S3之模型部署
  • C语言之结构体详解
  • 网站提示不安全?
  • IDEA性能优化的相关配置
  • java操作windows系统功能案例(一)
  • 中台战略思想与架构总结
  • finebi 新手入门案例
  • Word打印模板,打印效果更出众丨三叠云
  • 【合集】MQ消息队列——Message Queue消息队列的合集文章 RabbitMQ入门到使用
  • 新型Python环境与依赖管理工具——pipenv
  • 文章解读与仿真程序复现思路——电力系统自动化EI\CSCD\北大核心《基于分布鲁棒优化的广义共享储能容量配置方法》
  • 【Python】基础练习题_组合数据类型_2
  • debianubuntu的nvidia驱动升级
  • SQL server 基线安全加固操作
  • LeetCode(37)矩阵置零【矩阵】【中等】
  • AS 之 gradle 命令
  • excel合并单元格教程
  • HX3002入耳检测光感驱动调试-感0x08 寄存器溢出,不变化错误问题解决方法