lodash常用方法集合

安装npm install lodash --save
引入 import * as _ from ‘lodash’;
常用方法有以下:
1..last(array) //获取array中的最后一个元素
2.
.head(array) //获取数组 array 的第一个元素
2..sampleSize(collecttion, n) //从集合中随机获取n个元素;
3.
.sample(collection) //从集合中中获得一个随机元素
4..uniq(array) //数组去重
5.
.truncate([string=‘’], [options=]) //截取指定长度后加… 用于处理标题过长,如下
.truncate(‘hi-diddly-ho there_neighborino’,{
length:50 //最大允许长度,默认36
omission:‘等等’// 超出后的代替字符,默认……
separator:‘
’ //截断点,即在什么地方截断,可使用正则
})
6..cloneDeep(value) //深度复制
7.
.clone(value) //浅复制
8..shuffle(collection) //创建一个被打乱值的集合
9.
.times(n,function); //循环n次,每次迭代调用的函数
10. _.range([start=0], end, [step=1]) // 范围内数字组成的新数组 _.range(0, 20, 5); => [0, 5, 10, 15] //按照某种规律组成数组
11. _.find(collection,Array|Function|Object|string,fromIndex) //和js中的find功能相同,第一个返回真值的第一个元素。简单计算中用Array|Object|string,复杂的用function,用funciton时直接用js的find。fromIndex非必填,开始查找的位置(默认0);
12. _.findIndex(collection,Array|Function|Object|string,fromIndex) //和find类似,返回索引值而非属性;
13. _.filter(collection,Array|Function|Object|string) //过滤,用法和_find一样
14. .reject(collection, Array|Function|Object|string)//.filter的反向方法
15. _.map(collection, Array|Function|Object|string) //创建新的数组,用法和_find一样
16. _forEach(collection,function); //遍历,和js的forEach一样,唯一不一样的是length也循环;
17. _.includes(collection, value, [fromIndex=0]) //value包括Array|Object|string,js中只有string用includes,用法是:string.includes(searchvalue, start)
18. _.get(object, path, [defaultValue]) //object对象的path路径获取值。 如果解析 value 是 undefined 会以 defaultValue 取代,path包括 Array|string。
19. _.max(array) //数组中最大的值 ; js中Math.max(x…) 返回多个数中最大的值;
20. _.min(array) //数组中最小的值 ;
21. _.size(collection) //集合的长度包括array,object,string;而js中的length不能对object计算;
22. _.partition(collection,Array|Function|Object|string) //创建一个分成两组的元素数组,第一组包含predicate(断言函数)返回为 truthy(真值)的元素,第二组包含predicate(断言函数)返回为 falsey(假值)的元素;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值