-
debounce
var fn = debounce(() => { }, 250, { maxWait: 500 })
-
throttle
var fn = throttle(() => { }, 250)
-
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'
-
2121
lodash常见的方法
于 2023-11-24 14:05:45 首次发布