lodash--collection
演示版本:4.8.2
1.countBy
_.countBy([6.1,5.2,6.2],Math.floor); //{ '5': 1, '6': 2 }
2.event
_.every([1,'123'],Boolean); //true _.every([1,0,'123'],Boolean); //false
3.filter
var filter_model=[ {'user':'xiaoMing','age':23,'active':true}, {'user':'xiaoWang','age':21,'active':false} ]; _.filter(filter_model,function(o){ return !o.active; }); //[ { user: 'xiaoWang', age: 21, active: false } ]
4.find
var find_model=[ {'user':'xiaoMing','age':23,'active':true}, {'user':'xiaoWang','age':21,'active':false}, {'user':'xiaoXu','age':21,'active':false} ]; //注意,find返回第一个匹配到的对象 _.find(find_model,'active'); //{ user: 'xiaoMing', age: 23, active: true }
5.findLast
var findLast=_.findLast([1,2,3,4],function(obj){ return obj%2==1; }); //3 注意:和find一样,也是返回第一个匹配到的对象
6.flatMap
function flatMap_duplicate(n){ return [[n,n+1]] } _.flatMap([1,2],flatMap_duplicate); //[ [ 1, 2 ], [ 2, 3 ] ]
7.flatMapDeep
function flatMapDeep_duplicate(n){ return [[n,n+1]] } _.flatMapDeep([1,2],flatMapDeep_duplicate); //[ 1, 2, 2, 3 ]
8.flatMapDepth
function flatMapDepth_duplicate(n){ return [[[n,n+1]]] } _.flatMapDepth([1,2],flatMapDepth_duplicate,2); //[ [ 1, 2 ], [ 2, 3 ] ] _.flatMapDepth([1,2],flatMapDepth_duplicate,3); //[ 1, 2, 2, 3 ]
9.forEach
var forEach=_([1,2]).forEach(function(a){ console.log(a) });//记录每个值从左到右,并返回数组 /* 1 2 [ 1, 2 ] */ var forEach_2=_.forEach({'name':'me','age':23},function(v,k){ console.log(k); });//迭代的顺序不能保证 /* name age { name: 'me', age: 23 } */
10.forEachRight
var forEachRight=_.forEachRight([1,2],function(obj){ console.log(obj); }); /* 2 1 [ 1, 2 ] */
11.groupBy
_.groupBy([4.1,4.2,5.3],Math.floor); //{ '4': [ 4.1, 4.2 ], '5': [ 5.3 ] }
12.includes
_.includes([1,2,3],1); //true _.includes([1,2,3],1,1); //false _.includes({'name':'me','age':23},23); //true
13.invokeMap
_.invokeMap([[5,1,7],[8,6,7]],'sort'); //[ [ 1, 5, 7 ], [ 6, 7, 8 ] ]
14.keyBy
_.keyBy(keyBy_model,'dir'); /* { left: { dir: 'left', code: 97 }, right: { dir: 'right', code: 100 } } */