lodash库——封装好的collection集合函数
Lodash _.集合方法
集合collection
- Array数组
- set集合(存储的元素是有序不重复的)
- Map集合(存储的元素以键值对key:value的形式)
let array=['y','m','a',1,6]
let set={}
let map=[{'value1':1,'value2':2}]
countBy—返回组成对象
countBy(collection,[iteratee=_.identity])
创建构建集合对象
key———经过iterratee执行的结果
value———相同结果执行key的次数
let temp=[19.49,2.5,2.2,2.8,19.98,20.20]
let result=_.countBy(temp,Math.round);
console.log(result)
forEach方法—遍历元素
forEach(collection,[iteratee=_.identity])
调用iteratee遍历collection,可以访问key和value
let temp={'yma16':16,'china':1949}
let result=_.forEach(temp,(key,value)=>{
console.log('key:',key,'value',value)
})
console.log(result)
扩展
each(collection,[iteratee=_.identity])
作用于forEach相同
forEachRight(collection,[iteratee=_.identity])
从右到左开始遍历
let temp={'yma16':16,'china':1949}
let result=_.forEachRight(temp,(key,value)=>{
console.log('key:',key,'value',value)
})
console.log(result)
every方法—判断数据
every(collection,[predicate=_.identity])
根据predicate的定义判断,若遇到假值,则停止切返回boolean的false
返回的是boolea类型
let temp=[1,2,3,null]
let result=_.every(temp,Boolean)
console.log(result)
filter方法—留下真值
filter(collection,predicate=_.identity)
遍历集合,根据predicate过滤掉满足不满足的元素
留下满足的元素(真值)
返回过滤后的数组
let user=[
{'name':'yma16','active':true},
{'name':'yam16','active':false},
]
let result=_.filter(user,(o)=>{return o.active})
console.log(result)
注意:reject方法与filter相反
reject(collection,[predicate=_.identity])
留下不满足predicate的元素(假值)
let user=[
{'name':'yma16','active':true},
{'name':'yam16','active':false},
]
let result=_.reject(user,(o)=>{return o.active})
console.log(result)
find方法—查找满足条件的第一个元素
find(collection,[predicate=_.identity],[fromindex=0])
便利collection集合的元素,返回第一个满足条件的真值
let user=[
{'user':'yma16','active':true},
{'name':'yam16','active':false},
{'name':'chinese','active':true}
]
let result=_.find(user,o=>{return o.active})
console.log(result)
扩展
findLast(collection,[predicate=_.identity],[fromindex=0])
从右到左开始查找,查到即返回
let user=[
{'user':'yma16','active':true},
{'name':'yam16','active':false},
{'name':'chinese','active':true}
]
let result=_.findLast(user,o=>{return o.active})
console.log(result)
flatMap方法—创建同阶数组
flatMap(collection,[predicate=_.identity])
同阶数组(平级)
let temp=[6]
let result=_.flatMap(temp,o=>{return [o,o,o]})
console.log(result)
扩展
flatMapDeep(collection,[predicate=_.identity])
完全是同等级
flatMapDepth(collection,[predicate=_.identity],[depth=1])
指定执行次数
let temp=[[[[6]]]]
let result1=_.flatMap(temp,o=>{return [o,o,o]})
let result2=_.flatMapDeep(temp,o=>{return [o,o,o]})
let result3=_.flatMapDepth(temp,o=>{return [o,o,o]},2)
console.log(result1)
console.log(result2)
console.log(result3)
groupBy方法—创建对象
groupBy(collection,[iteratee=_.identity])
与countBy方法相似,创建构建集合对象
key——是遍历元素计算的结果
value——是生成key的数组(countBy是key的迭代次数)
let temp=[19.49,2.5,2.2,2.8,19.98,20.20]
let result=_.groupBy(temp,Math.round);
console.log(result)
includes方法—是否包括
includes(collection,value,[fromindex=0])
可以检查字符串的子串是否存在
返回boolean
let result=_.includes([1,2,3],1)
console.log(result)
invokeMap方法—path方法处理
invokeMap(collection,path,[args])
调用path上的方法处理集合元素,返回一个对象数组(调用方法得到的结果)
let temp=[[1,2,3,2,1,],['a','b','c','b','a']]
let result=_.invokeMap(temp,'sort')
console.log(result)
keyBy方法—创建对象(自定义key)
keyBy(collection,[iteratee=_.identity])
创建一个对象组成, key经过 iteratee 处理后返回的结果。 value是生成key的最后一个元素。iteratee(迭代函数)调用1个参数:(value)
let user=[
{'user':'yma16','active':1},
{'name':'yam16','active':2},
{'name':'chinese','active':3}
]
let result=_.keyBy(user,o=>{return o.active})
console.log(result)
map方法—创建数组
map(collection,[iteratee=_.identity])
返回进过迭代结果组成的数组
let temp=[3,5]
let result=_.map(temp,o=>{return o*o})
console.log(result)
orderBy方法—排序
orderBy(collection,[iteratee= _.identity],[orders])
回想mysql的orderb排序(asc、desc)
let user=[
{'name':'yma16','active':1},
{'name':'yma16','active':2},
{'name':'chinese','active':3}
]
let result=_.orderBy(user,['name','active'],['asc','desc'])
console.log(result)
sortBy(collection,[iteratee= _.identity],[orders])
创建一个元素数组迭代后升序
let user=[
{'name':'yma16','active':1},
{'name':'yma16','active':2},
{'name':'chinese','active':3}
]
let result=_.sortBy(user,o=>{return o.name})
console.log(result)
partiton方法—划分为数组
partition(collection,[iteratee= _.identity])
返回数组对象包含两个数组
第一个数组—true
第二个数组—false
let user=[
{'name':'yma16','active':true},
{'name':'yam16','active':false},
{'name':'chinese','active':true}
]
let result=_.partition(user,o=>{return o.active})
console.log(result)
reduce方法—返回累加后的值
reduce(collection, [iteratee=_.identity], [accumulator])
accumulator为第一个迭代的值(定义返回类型),遍历集合的元素,返回值进入下次迭代
let temp={'yma16':16,'yma17':17,'yma18':18}
let result=_.reduce(temp,(result,key,value)=>{
(result[value]||(result[value]=[])).push(key)
return result
},{})
console.log(result)
扩展
reduceRight(collection, [iteratee=_.identity], [accumulator])
从右边到左边
sample(随机元素)、shuffle(乱序)、size(长度)方法
sample(collection,[n=1])随机取n个元素
let temp=[1,2,3,4,5,6,7]
console.log(_.sample(temp))
console.log(_.shuffle(temp))
console.log(_.size(temp))