LoDash 中文版之 Array

             学习LoDash,发现基本都是英文网站,对于初学者十分不方便。我自己总结了一下,现与大家共享,有错误处或不妥之处,请大神给予意见。

1.

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

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

 按指定长度合并数组中的元素

2.

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

 删除数组元素中的 虚假值(false, null, 0, "", undefined,  NaN are falsey)

3.

_.difference([1, 2, 3], [5, 2, 10]);
// → [1, 3]

 在第一个数组元素中删除与第二个数组相同的元素

4.

_.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
// → [1, 2]

 找到几个数组中共有的元素,并输出。

5.

_.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]

 输出删除的指定位置元素(默认值为1),如果为0则返回原数组。

6.

_.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]

 同上 只是从右边开始。

7.

_.dropRightWhile([1, 2, 3], function(n) { return n > 1; });
// → [1]

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

// using the "_.property" callback shorthand
_.pluck(_.dropRightWhile(users, 'active'), 'user');
// → ['barney']

// using the "_.matches" callback shorthand
_.pluck(_.dropRightWhile(users, { 'status': 'away' }), 'user');
// → ['barney', 'fred']

 根据条件(users, 'active'),查找'user'中不满足条件的对象。

根据(users, { 'status': 'away' }),查找'user'中不满足条件的对象。

8.

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

_.findIndex(users, function(chr) { return chr.age < 40; });
// → 0

// using the "_.matches" callback shorthand
_.findIndex(users, { 'age': 1 });
// → 2

// using the "_.property" callback shorthand
_.findIndex(users, 'active');
// → 1

 输出反向满足条件的对象个数

9.

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

_.findLastIndex(users, function(chr) { return chr.age < 40; });
// → 2

// using the "_.matches" callback shorthand
_.findLastIndex(users, { 'age': 40 });
// → 1

// using the "_.property" callback shorthand
_.findLastIndex(users, 'active');
// → 0

 满足条件的对象个数

10.

_.first([1, 2, 3]);
// → 1

_.first([]);
// → undefined
输出第一个元素                 
_.rest([1, 2, 3]);
// → [2, 3]
输出除第一个元素的剩下元素。
_.last([1, 2, 3]);
// → 3
输出最后一个元素。
_.initial([1, 2, 3]);
// → [1, 2]
输出除最后一个元素的剩下元素。 

 11.

_.flatten([1, [2], [3, [[4]]]]);
// → [1, 2, 3, [[4]]];
就是输出一下这个数组
// using `isDeep`
_.flatten([1, [2], [3, [[4]]]], true);
// → [1, 2, 3, 4];
用 isDeep  把各个递归中的元素,放在一层递归中

 12.1

_.indexOf([1, 2, 3, 1, 2, 3], 2);
// → 1

// using `fromIndex`
_.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
// → 4

// performing a binary search
_.indexOf([4, 4, 5, 5, 6, 6], 5, true);
// → 2

 ,后数据首次出现的位置,计数从0开始。

12.2

_.sortedLastIndex([4, 4, 5, 5, 6, 6], 5); //
// → 4

 其出现的最后位置

13.

var array = [1, 2, 3, 1, 2, 3];
_.pull(array, 2, 3);
console.log(array);
// → [1, 1]

 原数组删除2, 3后输出

14.

var array = [5, 10, 15, 20];
var evens = _.pullAt(array, [1, 3]);

console.log(array);
// → [5, 15]

console.log(evens);
// → [10, 20]

 相对于_.pull多加个情况,但功能与其相反。

15.

var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) { return n % 2 == 0; });

console.log(array);
// → [1, 3]

console.log(evens);
// → [2, 4]

 一看便知

16.

_.sortedIndex([30, 50], 40);
// → 1

_.sortedIndex([4, 4, 5, 5, 6, 6], 5);
// → 2

var dict = { 'data': { 'thirty': 30, 'forty': 40, 'fifty': 50 } };

// using an iteratee function
_.sortedIndex(['thirty', 'fifty'], 'forty', function(word) {
  return this.data[word];
}, dict);
// → 1

// using the "_.property" callback shorthand
_.sortedIndex([{ 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
// → 1

 输出其插入的位置,但会自动判断比较前后。

17.

_.takeRight([1, 2, 3], 2);
// → [2, 3]

_.takeRight([1, 2, 3], 5);
// → [1, 2, 3]

_.takeRight([1, 2, 3], 0);
// → []

 据[1, 2, 3],创建一个新的数组(,后的数字决定从开始到哪个数字结束,没有的省略,默认值为 1)。

18.

var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
// → [['fred', 30, true], ['barney', 40, false]]

_.unzip(zipped);
// → [['fred', 'barney'], [30, 40], [true, false]]

 从两个数组的对应位置上取出数据并存放到新的数组中(一个位置一个数组)。

其实就是_.zip。

19.

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
// → [2, 3, 4]
去掉 0 1

_.xor([1, 2, 3], [5, 2, 1, 4]);
// → [3, 5, 4]

_.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
// → [1, 4, 5]

 都有的留下,就一个有的留下,其余删掉,并合并。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值