Last active
July 27, 2017 10:07
-
-
Save razbakov/d5e6d16c28c73ae38bc5039e6a554499 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Input | |
groupBy = ['color', 'size', '', ...] | |
data = [ | |
{ | |
"color": "red", | |
"size": "big", | |
"data": "1" | |
}, | |
{ | |
"color": "red", | |
"size": "big", | |
"data": "2" | |
}, | |
{ | |
"color": "red", | |
"size": "small", | |
"data": "3" | |
}, | |
{ | |
"color": "blue", | |
"size": "big", | |
"data": "4" | |
}, | |
]; | |
// Output | |
` | |
Color: Red | |
Size: big | |
1 | |
2 | |
Size: small | |
3 | |
Color: blue | |
Size: big | |
4 | |
` | |
// Step 1: Merge data into groups | |
groups = [ | |
{ | |
"color": "red", | |
"size": "big", | |
"ids": [1, 2] | |
}, | |
{ | |
"color": "red", | |
"size": "small", | |
"ids": [3] | |
}, | |
{ | |
"color": "blue", | |
"size": "big", | |
"ids": [4] | |
}, | |
] | |
// Step 2: Generate rows with types | |
rows = [ | |
{ | |
type: "group", | |
level: 0, | |
key: "color", | |
value: "red" | |
}, | |
{ | |
type: "group", | |
level: 1, | |
key: "size", | |
value: "big" | |
}, | |
{ | |
type: "row", | |
level: 2, | |
rows: [1,2] | |
}, | |
{ | |
type: "group", | |
level: 1, | |
key: "size", | |
value: "small" | |
}, | |
{ | |
type: "row", | |
level: 2, | |
rows: [3] | |
}, | |
{ | |
type: "group", | |
level: 0, | |
key: "color", | |
value: "blue" | |
}, | |
{ | |
type: "group", | |
level: 1, | |
key: "size", | |
value: "big" | |
}, | |
{ | |
type: "row", | |
level: 2, | |
rows: [4] | |
}, | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment