Lodash(数组篇)


Lodash


chunk

_.chunk ( arr ,num )
从前往后按num切割arr

_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]

_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]

compact

_.compact ( arr )
过滤arr内所有false, null, 0, "", undefined, and NaN .

_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

concat

_.concat ( arr , val )
val过滤调外一层的[]后拼入arr

var array = [1];
var other = _.concat(array, 2, [3], [[4]]);

console.log(other);
// => [1, 2, 3, [4]]

console.log(array);
// => [1]

difference

_.difference ( arr , [ val ] )
返回arr内与val内元素的差异

_.difference([2, 1], [2, 3]);
// => [1]

differenceBy

_.differenceBy ( arr , [ val ] , fun)
返回经过fun过滤后arr内与val内元素的差异

_.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2]

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

differenceWith

_.differenceWith ( arr , [ val ] , fun )
返回经过fun过滤后arr内与val内元素的差异

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

_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]

drop

_.drop ( arr , num )
从前往后弹出arrnum个元素

_.drop([1, 2, 3]);
// => [2, 3]

_.drop([1, 2, 3], 2);
// => [3]

_.drop([1, 2, 3], 5);
// => []

_.drop([1, 2, 3], 0);
// => [1, 2, 3]

dropRight

_.dropRight ( arr , num )
从后往前弹出arrnum个元素

_.dropRight([1, 2, 3]);
// => [1, 2]

_.dropRight([1, 2, 3], 2);
// => [1]

_.dropRight([1, 2, 3], 5);
// => []

_.dropRight([1, 2, 3], 0);
// => [1, 2, 3]

dropRightWhile

_.dropRightWhile ( arr , fun )
从后往前返回arr内满足fun的元素val

_.dropRightWhile(users, function(o) { return !o.active; });
// => objects for ['barney']

_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['barney', 'fred']

_.dropRightWhile(users, ['active', false]);
// => objects for ['barney']

_.dropRightWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']

dropWhile

_.dropWhile ( arr , fun )
从前往后返回arr内满足fun的元素val

var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': true }
];

_.dropWhile(users, function(o) { return !o.active; });
// => objects for ['pebbles']

_.dropWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['fred', 'pebbles']

_.dropWhile(users, ['active', false]);
// => objects for ['pebbles']

_.dropWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']

fill

_.fill ( arr , val , start , end )
从前往后依次将arr内元素替换为val
start为开始替换位置
end为结束替换位置
startend为空,则默认全部替换

var array = [1, 2, 3];

_.fill(array, 'a');
console.log(array);
// => ['a', 'a', 'a']

_.fill(Array(3), 2);
// => [2, 2, 2]

_.fill([4, 6, 8, 10], '*', 1, 3);
// => [4, '*', '*', 10]

findIndex

_.findIndex ( arr , fun , num )

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值