lodash.js 学习笔记

  1. 1. 参考链接

underscore.js 提供了一系列工具函数,而 lodash.js 可以认为是 underscore.js 的一个超集。

简单示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var _ = require('lodash');
// 去掉falsy值后的数组
_.compact([0, 1, false, 2, '', 3]);
// → [1, 2, 3]
// 找出数组中不同的值
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
// → [1, 3, 4]
// 根据条件找出数组元素的索引值,未找到则返回 -1
var characters = [
{ 'name': 'barney', 'age': 36, 'blocked': false },
{ 'name': 'fred', 'age': 40, 'blocked': true },
{ 'name': 'pebbles', 'age': 1, 'blocked': false }
];
_.findIndex(characters, function(chr) {
return chr.age < 20;
});
// → 2
// using "_.where" callback shorthand
_.findIndex(characters, { 'age': 36 });
// → 0
// using "_.pluck" callback shorthand
_.findIndex(characters, 'blocked');
// → 1
// 找出数组中相同的值
_.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
// → [1, 2]
// 找出数组中最后的值
_.last([1, 2, 3]);
// → 3
_.last([1, 2, 3], 2);
// → [2, 3]
_.last([1, 2, 3], function(num) {
return num > 1;
});
// → [2, 3]
// 找出某个值最后出现的索引
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
// → 4
// 从 第三个元素开始
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
// → 1
// 移除数组中指定的值
var array = [1, 2, 3, 1, 2, 3];
_.pull(array, 2, 3);
console.log(array);
// → [1, 1]
// 返回一个范围数组
// _.range([start=0], end, [step=1])
_.range(4);
// → [0, 1, 2, 3]
_.range(1, 5);
// → [1, 2, 3, 4]
_.range(0, 20, 5);
// → [0, 5, 10, 15]
_.range(0, -4, -1);
// → [0, -1, -2, -3]
_.range(1, 4, 0);
// → [1, 1, 1]
_.range(0);
// → []
// 移除数组中匹配条件的值
var array = [1, 2, 3, 4, 5, 6];
var evens = _.remove(array, function(num) { return num % 2 == 0; });
console.log(array);
// → [1, 3, 5]
console.log(evens);
// → [2, 4, 6]
// 切割数组,默认切割 1
// _.rest(array, [callback=1], [thisArg])
_.rest([1, 2, 3]);
// → [2, 3]
_.rest([1, 2, 3], 2);
// → [3]
_.rest([1, 2, 3], function(num) {
return num < 3;
});
// → [3]
// 更多实用函数见 http://lodash.com/docs

参考链接