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'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
- 1][y] == 3 - cur_player && !visited[x - 1][y]) { visited[x - 1Lodash 是一个 JavaScript 实用工具库,提供了许多常见的函数和方法来简化 JavaScript 开发。][y] = 1; if (has_liberty(x - 1, y)) { flag = 1; }下面是 Lodash 中常用的一些方法及其用法: 1. `_.chunk(array, [size])`: 将 else if (!check_liberty(x - 1, y)) { remove(x - 1, y); } } if数组分割成多个子数组,每个子数组包含指定大小的元素。 ```javascript _.chunk([1 (x < BOARD_SIZE - 1 && board[x + 1][y] == 3 - cur_player && !visited[x + , 2, 3, 4, 5], 2); // => [[1, 2], [3, 41][y]) { visited[x + 1][y] = 1; if (has_liberty(x + 1,], [5]] ``` 2. `_.compact(array)`: 创建一个新数组,其中不包含 falsey 值(false、 y)) { flag = 1; } else if (!check_liberty(x + 1, y)) { remove(x +null、0、""、undefined 和 NaN)。 ```javascript _.compact([0, 1, false, 2, '', 1, y); } } if (y > 0 && board[x][y - 1] == 3 -3]); // => [1, 2, 3] ``` 3. `_.concat(array, [values])`: 将一个或 cur_player && !visited[x][y - 1]) { visited[x][y - 1] = 1; if (has_liberty(x, y - 1)) { flag = 1; } else if (!check_liberty(x, y -多个值连接到数组的末尾。 ```javascript var array = [1]; var other = _.concat(array, 2, [3], [[4]]); console.log(other); // => [1, 2, 3, [4]] console.log(array); // 1)) { remove(x, y - 1); } } if (y < BOARD_SIZE - 1 && board => [1] ``` 4. `_.difference(array, [values])`: 创建一个数组,其中不包含在其他给定[x][y + 1] == 3 - cur_player && !visited[x][y + 1]) { visited[x][y数组中的元素。 ```javascript _.difference([2, 1], [2, 3]); // => [1] `` + 1] = 1; if (has_liberty(x, y + 1)) { flag = 1; ` 5. `_.drop(array, [n=1])`: 创建一个切片数组,去掉数组的前 n 个元 } else if (!check_liberty(x, y + 1)) { remove(x, y + 1); } } return flag; } // 下一步 void next() { char input[10]; int x, y; int forbidden_hand素。 ```javascript _.drop([1, 2, 3]); // => [2, 3] _.drop([1, 2, 3], 2); // => [3] _.drop([1, 2, 3], 5); // = 0; printf("轮到 %s 下棋:\n", cur_player == BLACK ? "黑方" : "白 => [] _.drop([1, 2, 3], 0); // => [1, 2, 3] ``` 方"); while (1) { printf("请输入落子位置(如A1):"); scanf("%s", input); 6. `_.find(array, [predicate=_.identity], [fromIndex=0])`: 遍历数组元素,返回第一个 x = input[1] - '1'; y = input[0] - 'A'; if (x < 0通过 predicate 判断为真的元素。 ```javascript var users = [ { 'user': 'barney', 'active': true || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) { printf("位置无效!\n"); continue }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false; } if (!put(x, y)) { continue; } if (check_forbidden_hand(x, y)) { } ]; _.find(users, function(o) { return o.active; }); // => { 'user': 'barney', 'active': true printf("禁手!\n"); board[x][y] = EMPTY; continue; } if (!check_lib } // The `_.matches` iteratee shorthand. _.find(users, { 'user': 'fred', 'active': false }); // =>erty(x, y)) { printf("提子!\n"); count_score(); } break; } cur { 'user': 'fred', 'active': false } // The `_.matchesProperty` iteratee shorthand. _.find(users, ['active_player = 3 - cur_player; } // 游戏结束 void game_over() { count_score(); printf("黑方得', false]); // => { 'user': 'fred', 'active': false } // The `_.property` iteratee shorthand. _.find分:%d\n", black_score); printf("白方得分:%d\n", white_score); if (black_score > white(users, 'active'); // => { 'user': 'barney', 'active': true } ``` 7. `_.flatten(array)_score) { printf("黑方胜利!\n"); } else if (black_score < white_score) { printf("`: 创建一个新数组,将多维数组展开一层。 ```javascript _.flatten([1, [2, [3,白方胜利!\n"); } else { printf("平局!\n"); } } int main() { [4]], 5]]); // => [1, 2, [3, [4]], 5] _.flatten([1, [ init_board(); while (1) { print_board(); next(); if (black_score + white_score == BOARD_SIZE * BOARD_SIZE) { game_over(); break; } } return 0; } ``` 使用该程序可以在2, [3, [4]], 5]], true); // => [1, 2, 3, [4], 5] ``` 8. `_.map(collection, [iteratee=_.identity])`: 遍历集合中的每个元素,并codeblocks中进行围棋双人对战,并且实现了落子,吃子,禁手规则和判断胜负等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值