lodash source learn【lodash源码学习】

lodash-source-learn

“Array” Methods

1
2
3
_.chunk(array, [size=1])

chunk(array, [size=1])

Creates an array of elements split into groups the length of size. If array can’t be split evenly, the final chunk will be the remaining elements.

创建一个元素数组,将元素分成大小的长度。如果数组不能均匀分割,则最终的块将是剩余的元素。

chunk

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
function chunk (array, size)  {
// 获取size, 如果小于0 取值为0
size = Math.max(size, 0)
// 获取数组长度 如果是null 为0, 否则为数组长度
const length = array === null ? 0 : array.length
// 如果没有长度或者切割小于0返回[]
if (!length || size < 1) {
return []
}
// 开始 index
let index = 0
// 数组 index
let resIndex = 0

// 数组数据 new Array 长度为 length/size 向上取整
const result = new Array(Math.ceil(length / size))

// 数组每个下标添加内容 开始index index+=size 结束index
while (index < length) {
// 这里的 _.slice 使用了 array.slice 代替
result[resIndex++] = array.slice(index, index+=size)
}
return result
}

// const resChunk = chunk([1,2,3,4,5,6,7], 2)
// console.log(resChunk);


function chunkLite(array, size) {
// 获取长度
const length = array.length
// 如果没有长度或者切割小于0返回[]
if (!length || size < 1) return []
let index = 0 // 开始 index
let resIndex = 0 // 数组 index
let result = [] // 数组数据
// 循环 开始的索引小于数组长度执行
// 数组每个下标添加内容 开始index index+=size 结束index
while(index < length) result[resIndex++] = array.slice(index, index+=size)
return result
}

const resChunkLite = chunkLite([1,2,3,4,5,6,7], 1)
console.log(resChunkLite);
const resChunkLite1 = chunkLite([1,2,3,4,5,6,7], 2)
console.log(resChunkLite1);
const resChunkLite2 = chunkLite([1,2,3,4,5,6,7], 3)
console.log(resChunkLite2);
const resChunkLite3 = chunkLite([1,2,3,4,5,6,7], 4)
console.log(resChunkLite3);
const resChunkLite4 = chunkLite([1,2,3,4,5,6,7], 5)
console.log(resChunkLite4);

// [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ] ]
// [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7 ] ]
// [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7 ] ]
// [ [ 1, 2, 3, 4 ], [ 5, 6, 7 ] ]
// [ [ 1, 2, 3, 4, 5 ], [ 6, 7 ] ]

// [Done] exited with code=0 in 0.093 seconds

todo _.slice 方法会在后面继续学习

compact

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

Creates an array with all falsey values removed. The values false, null, 0, “”, undefined, and NaN are falsey.

创建一个删除了所有falsey值的数组。 值false,null,0,“”,undefined和NaN都是假的。

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
const compact = array => {
let resIndex = 0
let result = []
if (array === null) return result
for (value of array) {
if (value) { // 如果if为falsey值不写入
result[resIndex++] = value
}
}
return result
}

let compact1 = compact([1,2,3])
console.log(compact1)

let compact2 = compact([1,2,null,false, null, 0 , '',3,4])
console.log(compact2)
// [1, 2, 3]
// [1, 2, 3, 4]

// lite
const compact = array => {
let resIndex = 0
let result = []
for (value of array)
if (value) result[resIndex++] = value
return result
}

let compact1 = compact([1,2,3])
console.log(compact1)

let compact2 = compact([1,2,null,false, null, 0 , '',3,4])
console.log(compact2)

// [1, 2, 3]
// [1, 2, 3, 4]
打赏测试
显示 Gitment 评论