lodash常见的方法

xor

javascript 复制代码
xor([1, 2], [2, 3], [3, 4]) // [1, 4]
javascript 复制代码
xorBy([{ x: 1 }], [{ x: 2 }, { x: 1, y: 1 }], 'x'); // [ { x: 2 } ]
javascript 复制代码
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

javascript 复制代码
escape('aa, bb, &<>" cc');//aa, bb, &amp;&lt;&gt;&quot; cc

wrap

javascript 复制代码
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

javascript 复制代码
var object = { 'a': [
                      { 'b': { 'c': 3 } }, 
                      4
                    ] 
             };
at(object, ['a[0].b.c', 'a[1]']) // => [ 3, 4 ]

before

javascript 复制代码
const fn = before(3, () => {
  console.log(11111);
})
fn() // 11111
fn() // 11111
fn() // 没有反应
fn() // 没有反应

after

javascript 复制代码
var done = after(2, ()=>{ console.log(1) });
forEach(['profile', 'settings'], function(type) {
  asyncSave({ 'type': type, 'complete': done });
});

chain

javascript 复制代码
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

javascript 复制代码
chunk(['a', 'b', 'c', 'd', 'e'], 2) // [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e' ] ]

clone

javascript 复制代码
var a = [{ 'a': 1 }, { 'b': 2 }];
var b = clone(a);
b[0] === a[0] // true

cloneDeep

javascript 复制代码
var a = [{ 'a': 1 }, { 'b': 2 }];
var b = cloneDeep(a);
b[0] === a[0] // false

isEmpty

javascript 复制代码
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

javascript 复制代码
isEqual({}, {}) // true

intersection

javascript 复制代码
intersection([2, 1], [4, 2], [1, 2]); // => [2]
javascript 复制代码
intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor); // => [2.1]
intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 1 }]
javascript 复制代码
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

javascript 复制代码
uniq([2, 1, 2]) // => [2, 1]
javascript 复制代码
uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
javascript 复制代码
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

javascript 复制代码
union([2], [1, 2]) // => [2, 1]
javascript 复制代码
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 }]
javascript 复制代码
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

javascript 复制代码
difference([1, 2, 3], [1, 3, 4, 5]); // [2]
javascript 复制代码
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 }]
javascript 复制代码
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);// => [{ 'x': 2, 'y': 1 }]

flatten

javascript 复制代码
flatten([1, [2, [3, [4]], 5]]);// => [1, 2, [3, [4]], 5]
javascript 复制代码
flattenDeep([1, [2, [3, [4]], 5]]);// => [1, 2, 3, 4, 5]
javascript 复制代码
var array = [1, [2, [3, [4]], 5]];
flattenDepth(array, 1); // => [1, 2, [3, [4]], 5]
flattenDepth(array, 2); // => [1, 2, 3, [4], 5]
相关推荐
古夕5 分钟前
JS 模块化
前端·javascript
wandongle5 分钟前
HTML面试整理
前端·面试·html
liucan2336 分钟前
JS执行速度似乎并不比Swift或者C语言慢
前端·ios
一只小风华~8 分钟前
HTML前端开发:JavaScript 常用事件详解
前端·javascript·html
Revol_C11 分钟前
【调试日志】我只是用wangeditor上传图片而已,页面咋就崩溃了呢~
前端·vue.js·程序员
天天码行空14 分钟前
GruntJS-前端自动化任务运行器从入门到实战
前端
smallzip15 分钟前
node大文件拷贝优化(显示进度)
前端·性能优化·node.js
mouseliu16 分钟前
python之二:docker部署项目
前端·python
要加油哦~27 分钟前
css | class中 ‘.‘ 和 ‘:‘ 的使用 | 如,何时用 &.is-selected{ ... } 何时用 &:hover{...}?
前端·css
不浪brown1 小时前
开源!矢量建筑白模泛光特效以及全国77个大中城市的矢量shp数据获取!
前端·cesium