lodash常见的方法

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 '

' + func(text) + '

'
; }); p('aa, bb, & cc') //

fred%2C%20barney%2C%20%26%20pebbles

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 done = after(2, ()=>{ console.log(1) });
forEach(['profile', 'settings'], function(type) {
  asyncSave({ 'type': type, 'complete': done });
});

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]

你可能感兴趣的:(前端)