-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAn Utility to group items in order.md
182 lines (125 loc) · 3.8 KB
/
An Utility to group items in order.md
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Toggle
## Toggle with only two choices
E.g. toggle a switch whose status can only be `on` and `off`. In the case, we can use `boolean`
```javascript
let status = true;
function toggle() {
status = !status;
}
for(let i = 0; i < 10; i++) {
toggle();
console.log(status ? 'on' : 'off');
}
```
## Toggle with more than two choices
E.g. toggle traffic lights. In the case, we can use a counter and increment the counter at each toggle and use `remainder` to get the active light.
```javascript
let counter = 0;
function toggle() {
counter++
}
function getActiveLight() {
let remainder = counter % 3;
switch (remainder) {
case 0:
return 'red';
case 1:
return 'yellow';
case 2:
return 'green';
}
}
for(let i = 0; i < 10; i++) {
toggle();
console.log(getActiveLight());
}
```
# Grouping
The idea of grouping a list of items into subgroups is similar to the `toggle` example above where we make use of `quotient` and `remainder`
## Z Ordering
> left-to-right, top-to-bottom
E.g.
[1,2,3,4,5,6,7] with 3 groups will result in:
```
[[1, 2, 3], [4, 5, 6], [7]]
```
i.e.
```
1 2 3
4 5 6
7
```
### Implementation
#### Background
* An array `arr`
* number of groups/colums `numOfColumns`
#### Pattern
```javascript
A B C D
E F G H
I
```
Given `numOfColumns`, the item with index `idx` should be placed on rowIndex `Math.floor(index / numOfColumns)` and columnIndex `idx % numOfColumns`. For the example, `G` has index `6` and `numOfColumns` is 4. Therefore,
* rowIndex of `G` would be `Math.floor(idx / numOfColumns) = Math.floor(6 / 4) = 1 `
* columnIndex of `G` would be `idx % numOfColumns = 6 % 4 = 2`
=> `G` would be on the third column of the second row. (Note: index starts from zero)
#### Code
```javascript
const result = [];
arr.forEach((it, index) => {
let rowIndex, columnIndex;
rowIndex = Math.floor(index / numOfColumns);
columnIndex = index % numOfColumns;
result[rowIndex] = result[rowIndex] || [];
result[rowIndex][columnIndex] = it;
});
```
## N Ordering
> top-to-bottom -> left-to-right
E.g.
[1,2,3,4,5,6,7] with 3 groups will result in:
```
[[1, 4, 7], [2, 5], [3, 6]]
```
i.e.
```
1 4 7
2 5
3 6
```
### Implementation
#### Background
* An array `arr`
* number of groups/colums `numOfColumns`
#### Thoughts
`Z` ordering is a bit counter-intuitive. However, it still follow a pattern similar to `N` ordering. The difference lays on
* The concept of row and column is the reverse of `N` ordering
* How to deal with `quotient` and `remainder`
#### Pattern
```javascript
A C E G
B D F H
```
Given `numOfColumns`, the number of rows `numOfRows` that we need to have to hold the items would be `Math.ceil(arr.length / numOfColumns)`. The item with index `idx` should be placed on rowIndex `idx % numOfRows` and columnIndex `Math.floor(idx / numOfRows)`. For the example, `G` has index `6` and `numOfColumns` is 4. Therefore,
* `numOfRows` would be `Math.ceil(arr.length / numOfColumns) = Math.ceil(8 / 4) = 2`
* rowIndex of `G` would be `idx % numOfRows = 6 % 2 = 0`
* columnIndex of `G` would be `Math.floor(idx / numOfRows) = Math.floor(6 / 2) = 3`
=> `G` would be on the fourth column of the first row. (Note: index starts from zero)
#### Code
```javascript
let result = [];
const numOfRows = Math.ceil(arr.length / numOfColumns);
arr.forEach((it, index) => {
let rowIndex, columnIndex;
rowIndex = index % numOfRows;
columnIndex = Math.floor(index / numOfRows);
result[rowIndex] = result[rowIndex] || [];
result[rowIndex][columnIndex] = it;
});
```
# Source Code
[Codepen](https://codepen.io/n0rush/pen/jRmbxX)
[github](https://github.com/n0ruSh/to-grid)
[npm](https://www.npmjs.com/package/to-grid)
# Notice
* If you want to follow the latest news/articles for the series of my blogs, Please [「Watch」](https://github.com/n0ruSh/blogs/)to Subscribe.