Lodash常用方法

Lodash常用方法

Lodash 是一个一致性、模块化、高性能的 JavaScript 实用工具库。由于内部封装了众多实用的方法,在许多项目中被使用,以下介绍了了在项目中使用频率较多的一些方法。

1.obj

1.findkey

对象的find方法,返回第一个符合条件的对象的key。

const obj = { a: 10, b: 20, c: 30 };
const result = findKey(obj, (e) => e < 20);
// result='a'
2.get

一般通过这个方法获取嵌套层数较深的数据,这个方法对在ts中查找嵌套数据有奇效。

const obj= { a: [{ b: { c: 30 } }] };
const result = get(obj, 'a[0].b.c');
// result=30
3.has

判断对象中是否存在查找的属性

const obj = { a: 10, b: { c: 30 } };
const result1 = has(obj, 'a');
const result2 = has(obj, 'b.c');
const result3 = has(obj, 'b.a');
// result1=true
// result2=true
// result3=false
4.keys

将对象的属性转换为一个数组

const obj = { a: 10, b: 20, c: 30 };
const result = keys(obj);
// result=['a','b','c']
5.values

将对象的值转换为一个数组

const obj = { a: 10, b: 20, c: 30 };
const result = keys(obj);
// result=[10,20,30]
6.mapValues

获取嵌套对象的某一层的一个具体值好方法

const obj = {
     a: { name: 'alex', age: 15 },
     b: { name: 'bear', age: 20 },
     c: { name: 'Chile', age: 25 },
};
const result = mapValues(obj,(e)=>(e.name));
// result = { a: 15, b: 20, c: 25 };
7.omit

忽略对象中传入的属性,相当于delete方法,但不改变原对象,返回的是新对象

const obj = { a: 10, b: 20, c: 30 };
const result = omit(obj, ['a', 'b']);
// result1={ c: 30 }
8.pick

和omit相反,这个方法可以选择对象中传入的属性

const obj = { a: 10, b: 20, c: 30 };
const result = pick(obj, ['a', 'b']);
// result1={ a: 10, b: 20 }

2.Array

1. remove

和array.filter()的作用相反,移除数组中符合条件的值

const array = [10, 20, 30];
const result = remove(array, (e)=>(e < 20));
// result =[20, 30]
2. uniq

数组去重

const array = [10, 20, 30, 20, 10];
const result = uniq(array);
// result =[10, 20, 30]
3. uniqBy

常用做在对象数组中根据对象中某个属性去重

const array = [
   { a: 10, b: 10 },
   { a: 20, b: 20 },
   { a: 10, b: 30 },
   { a: 30, b: 10 },
];
const result1 = uniqBy(array,'a');
const result2 = uniqBy(array,'b');
// result1 = [{ a: 10, b: 10 },{ a: 20, b: 20 },{ a: 30, b: 10 }];
// result2 = [{ a: 10, b: 10 },{ a: 20, b: 20 },{ a: 10, b: 30 }];

3.其它

1. castArray

将内部值强行转换为数组

const result = castArray(1);
// result = [1]
 
const result = castArray({ 'a': 1 });
// result = [{ 'a': 1 }]
 
const result = castArray('abc');
// result = ['abc']
 
const result = castArray(null);
// result = [null]
 
const result = castArray(undefined);
// result = [undefined]
 
const result = castArray();
// result = []
2. cloneDeep

将数据深拷贝,同 JSON.parse(JSON.stringify(data))

const data = [10, 20, 30];
const result = cloneDeep(array);
result[0] = 40;
// data = [10, 20, 30]
// result = [40, 20, 30]
3. isNil

判断数据是否为undefined或null

const result = isNil(null);
// result = true;

const result = isNil(undefined);
// result = true;

const result = isNil(0);
// result = false;
4. camelCase

将字符串转换为驼峰写法

const result = camelCase('Foo Bar');
// result = 'fooBar'
 
const result = camelCase('--foo-bar--');
// result = 'fooBar'
 
const result = camelCase('__FOO_BAR__');
// result = 'fooBar'
5. truncate

限定字符串长度并截断字符,超出部分转换为"…"。转换的字符可以被替换"…"是默认值

const str = 'wryyyyyyy';
const truncatedStr = truncate('str', { length: 5 });
// truncatedStr = 'wryyy...'
 
const truncatedStr = truncate('str', { length: 5, omission: '[...]'});
// truncatedStr = 'wryyy[...]'

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值