-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.js
48 lines (42 loc) · 1.69 KB
/
array.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
//Remove falsy values from array
const a = [1, 2, "b", 0, {}, "", NaN, 3, undefined, null, 5, []];
const b = a.filter(Boolean); //[1, 2, "b", {}, 3, 5, []];
//Generate array
const arr = [...Array(10).keys()] //[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
//Remove duplicate values in an array
const removeDuplicates = (arr) => [...new Set(arr)];
console.log(removeDuplicates([1, 2, 2, 3, 3, 4, 4, 5, 5, 6])); // [1, 2, 3, 4, 5, 6]
//Chunk 1#
const values = [3, 4, 5, 2, 6, 3, 2, 25, 5, 2, 45, 7];
const chunkSize = 5;
const removeArrayFrom = false;
const chunks = Array.from({ length: Math.ceil(values.length / chunkSize) })
.map((_, index) => removeArrayFrom ?
values.splice(0, chunkSize) :
values.slice(index * chunkSize, chunkSize * (index + 1)));
/*Result
chunks = [[3, 4, 5, 2, 6], [3, 2, 25, 5, 2], [45, 7]]
if removeArrayFrom = true
values = []
if removeArrayFrom = false
values = [3, 4, 5, 2, 6, 3, 2, 25, 5, 2, 45, 7]
*/
//Chunk 2 by lodash chucnk#
const values = [3, 4, 5, 2, 6, 3, 2, 25, 5, 2, 45, 7];
const chunkSize = 5;
const chunks = new Array(Math.ceil(values.length / chunkSize));
let index = 0, cindex = 0;
while (index < values.length)
chunks[cindex++] = values.slice(index, (index += chunkSize))
/*Result
chunks = [[3, 4, 5, 2, 6], [3, 2, 25, 5, 2], [45, 7]]
values = [3, 4, 5, 2, 6, 3, 2, 25, 5, 2, 45, 7]
*/
//Chunk 3 lazy loading
function* lazyLoadArray(data, chunkSize = 5) {
for (let i = 0; i < data.length; i += chunkSize)
yield data.slice(i, i + chunkSize);
}
const lazyLoader = lazyLoadArray([3, 4, 5, 2, 6, 3, 2, 25, 5, 2, 45, 7]);
for (const chunk of lazyLoader)
console.log(chunk);