美文网首页
lodash入门

lodash入门

作者: 彭奕泽 | 来源:发表于2018-09-13 00:43 被阅读0次

1. _.orderBy

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 34 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 36 }
];

_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
// 先按照'user'的升序排序,若相同则按age的降序排序
// 数据结构还是和之前user的数组一样,只是顺序变了
  • 助教君项目
    把name===English的放前面
subjects = _.orderBy(subjects, a => a.en_name === 'english' ? 1 : 2)

a.en_name === 'english'的值是1,其余的都是2,然后从小到大排序,这样english就放到前面了

  • 艺考项目
a = [{canGrade: 0,canScore: 0,}, {canGrade: 1,canScore: 0,}, {canGrade: 1,canScore: 1,}, {canGrade: 0,canScore: 1,}]
_.orderBy(a, ['canGrade', 'canScore'], ['desc', 'desc'])

0: {canGrade: 1, canScore: 1}
1: {canGrade: 1, canScore: 0}
2: {canGrade: 0, canScore: 1}
3: {canGrade: 0, canScore: 0}

18. sortBy

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 34 }
];
 
_.sortBy(users, [o => o.user]);
// [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
 
_.sortBy(users, ['user', 'age']);
// => [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
  • 助教君项目
    把name===English的放前面
let subjects = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}, {name: 'English'}]
subjects = _.sortBy(subjects, o => o.name !== 'English')
// [{name: 'English'}, {name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}]
  • 艺考项目
this.allExamList = _.sortBy(
  this.allExamList,
  [(o: any) => -o.canGrade,  (o: any) => -o.canScore]
);

让canGrade === 1的时候要放在前面,再让canScore === 1的排在第二,两个都没有的排在最后,加个负号是因为sortBy默认从小到大排,所以加个负号反过来,从大到小排

2. _.keyBy

var array = [
  { 'dir': 'left', 'code': 97 },
  { 'dir': 'right', 'code': 100 },
  { 'dir': 'pyz', 'code': 101 },
];

_.keyBy(array, 'dir');
// 数据结构会变为对象
// {
//  left: { 'dir': 'left', 'code': 97 },
//  right:  { 'dir': 'right', 'code': 100 },
//  pyz: { 'dir': 'pyz', 'code': 101 },
// }

3. _.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'] }

4. _.map

function square(n) {
  return n * n;
}

_.map([4, 8], square);
// => [16, 64]

_.map({ 'a': 4, 'b': 8 }, square);
// => [16, 64]

var users = [
  { 'user': 'barney' },
  { 'user': 'fred' }
];
_.map(users, 'user');

5. _.forEach

_.forEach([1, 2], function(value) {
  console.log(value);
});
// 1 2

_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  console.log(key);
});
// a b

6. _.flatten

_.flatten(['p', ['y', ['z', ['j']], 'p']]);
// ["p", "y", Array(2), "p"],貌似只能打平第一层

7. _.unipBy

_.uniqBy([2.1, 1.2, 2.3, 2.4, 2.8, 2.9, 3.2], Math.floor);
// [2.1, 1.2, 3.2]

_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// [{ 'x': 1 }, { 'x': 2 }]

8. _.sample

_.sample([1,'p', 3, 4]);
// 好像是随机选出一个数

9. _.shuffle

_.shuffle([1, 2, 3, 4]);
// 洗牌的意思,就是打乱

10. _.clone

var objects = [{ 'a': 1 }, { 'b': 2 }];
 
var shallow = _.clone(objects);
console.log(shallow === objects);
// false
console.log(shallow[0] === objects[0]);
// true

只能深复制第一层

11. _.cloneDeep

var objects = [{ 'a': 1 }, { 'b': 2 }];
 
var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false

这就是深复制

12. _.isEqual

var object = { 'a': 1 };
var other = { 'a': 1 };
 
_.isEqual(object, other);
// => true
 
object === other;
// => false

13. minBy

var objects = [{ 'n': 1 }, { 'n': 2 }];

_.minBy(objects, 'n');
// => { 'n': 1 }

14. _.sumBy

var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
 
_.sumBy(objects, 'n');
// => 20

15. _.merge

var object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};
 
var other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};
 
_.merge(object, other);
// { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

16. _.pick

var object = { 'a': 1, 'b': '2', 'c': 3 };
 
_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }

17. omit

var object = { 'a': 1, 'b': '2', 'c': 3 };
 
_.omit(object, ['a', 'c']);
// => { 'b': '2' }

反向选择

19. intersection

寻找两个数组里相同的数据

_.intersection([2, 1], [2, 3]);
// [2]

20. uniqBy 去重

_.uniqBy([2, 1, 2, 2, 2, 2, 45, 45, 60])
// [2, 1, 45, 60]

相关文章

  • lodash入门

    1. _.orderBy 助教君项目把name===English的放前面 a.en_name === 'engl...

  • lodash入门

    简介 Lodash是一个著名的javascript原生库,不需要引入其他第三方依赖。是一个意在提高开发者效率,提高...

  • lodash入门

    参考资料:https://www.cnblogs.com/jin-zhe/p/9703413.html Lodas...

  • Lodash使用入门

    项目里用到了Lodash。感觉有些还是很好用。就自己开研究了下。 参考链接: Lodash 中文文档 Lodash...

  • lodash入门指南

    相信每一位开发同学都会有一套自己积累的工具函数。但是在团队开发中,如何每个人使用自己的工具函数会导致代码不规范。所...

  • 4. webpack笔记4

    it老马webpack 入门教程; 我们按照这个走一遍. 起步,,引入lodash 报错. import _ fo...

  • lodash 常用

    uniapp使用lodash 安装lodash: npm i lodash -S

  • [分享] 最流行的 10 个 JavaScript 库

    1. Lodash https://github.com/lodash/lodash[https://github...

  • 前端常用插件工具类库汇总

    1、函数库 Lodash : https://github.com/lodash/lodash[https://g...

  • 2018-05-26 经常用到的lodash 函数

    lodash的官方文档: https://lodash.com/docs/4.17.10lodash的中文文档: ...

网友评论

      本文标题:lodash入门

      本文链接:https://www.haomeiwen.com/subject/qnrigftx.html