I want to create a dynamic hierarchy based on my groups and rollup sum to top level, also if I am providing a large amount of data browser is getting hang.
I have the below data:
JavaScript
x
213
213
1
var data = [
2
{
3
"country":"Spain",
4
"orderNumber":10394,
5
"year":2018,
6
"countrycode":"es",
7
"quantityOrdered":30,
8
"priceEach":60.28,
9
"productName":"1950's Chicago Surface Lines Streetcar",
10
"productLine":"Trains",
11
"sortkey":3,
12
"productCode":"S32_3207",
13
"month":"March",
14
"extendedPrice":1808.4,
15
"orderDate":"2018-03-15 00:00:00"
16
},
17
{
18
"country":"France",
19
"orderNumber":10395,
20
"year":2018,
21
"countrycode":"fr",
22
"quantityOrdered":32,
23
"priceEach":105.33,
24
"productName":"1972 Alfa Romeo GTA",
25
"productLine":"Classic Cars",
26
"sortkey":3,
27
"productCode":"S10_4757",
28
"month":"March",
29
"extendedPrice":3370.56,
30
"orderDate":"2018-03-17 00:00:00"
31
},
32
{
33
"country":"France",
34
"orderNumber":10395,
35
"year":2018,
36
"countrycode":"fr",
37
"quantityOrdered":33,
38
"priceEach":69.12,
39
"productName":"2001 Ferrari Enzo",
40
"productLine":"Classic Cars",
41
"sortkey":3,
42
"productCode":"S12_1108",
43
"month":"March",
44
"extendedPrice":2280.96,
45
"orderDate":"2018-03-17 00:00:00"
46
},
47
{
48
"country":"France",
49
"orderNumber":10395,
50
"year":2018,
51
"countrycode":"fr",
52
"quantityOrdered":46,
53
"priceEach":123.76,
54
"productName":"Diamond T620 Semi-Skirted Tanker",
55
"productLine":"Trucks and Buses",
56
"sortkey":3,
57
"productCode":"S50_1392",
58
"month":"March",
59
"extendedPrice":5692.96,
60
"orderDate":"2018-03-17 00:00:00"
61
},
62
{
63
"country":"France",
64
"orderNumber":10395,
65
"year":2018,
66
"countrycode":"fr",
67
"quantityOrdered":45,
68
"priceEach":199.49,
69
"productName":"1962 City of Detroit Streetcar",
70
"productLine":"Trains",
71
"sortkey":3,
72
"productCode":"S50_1514",
73
"month":"March",
74
"extendedPrice":8977.05,
75
"orderDate":"2018-03-17 00:00:00"
76
},
77
{
78
"country":"USA",
79
"orderNumber":10396,
80
"year":2018,
81
"countrycode":"us",
82
"quantityOrdered":33,
83
"priceEach":185.13,
84
"productName":"1969 Ford Falcon",
85
"productLine":"Classic Cars",
86
"sortkey":3,
87
"productCode":"S12_3891",
88
"month":"March",
89
"extendedPrice":6109.29,
90
"orderDate":"2018-03-23 00:00:00"
91
},
92
{
93
"country":"USA",
94
"orderNumber":10396,
95
"year":2018,
96
"countrycode":"us",
97
"quantityOrdered":33,
98
"priceEach":159.81,
99
"productName":"1903 Ford Model A",
100
"productLine":"Vintage Cars",
101
"sortkey":3,
102
"productCode":"S18_3140",
103
"month":"March",
104
"extendedPrice":5273.73,
105
"orderDate":"2018-03-23 00:00:00"
106
},
107
{
108
"country":"USA",
109
"orderNumber":10396,
110
"year":2018,
111
"countrycode":"us",
112
"quantityOrdered":24,
113
"priceEach":89.75,
114
"productName":"Collectable Wooden Train",
115
"productLine":"Trains",
116
"sortkey":3,
117
"productCode":"S18_3259",
118
"month":"March",
119
"extendedPrice":2154,
120
"orderDate":"2018-03-23 00:00:00"
121
},
122
{
123
"country":"USA",
124
"orderNumber":10396,
125
"year":2018,
126
"countrycode":"us",
127
"quantityOrdered":45,
128
"priceEach":105.32,
129
"productName":"1904 Buick Runabout",
130
"productLine":"Vintage Cars",
131
"sortkey":3,
132
"productCode":"S18_4522",
133
"month":"March",
134
"extendedPrice":4739.4,
135
"orderDate":"2018-03-23 00:00:00"
136
},
137
{
138
"country":"USA",
139
"orderNumber":10396,
140
"year":2018,
141
"countrycode":"us",
142
"quantityOrdered":49,
143
"priceEach":116.75,
144
"productName":"18th century schooner",
145
"productLine":"Ships",
146
"sortkey":3,
147
"productCode":"S24_2011",
148
"month":"March",
149
"extendedPrice":5720.75,
150
"orderDate":"2018-03-23 00:00:00"
151
},
152
{
153
"country":"USA",
154
"orderNumber":10396,
155
"year":2018,
156
"countrycode":"us",
157
"quantityOrdered":27,
158
"priceEach":83.2,
159
"productName":"1912 Ford Model T Delivery Wagon",
160
"productLine":"Vintage Cars",
161
"sortkey":3,
162
"productCode":"S24_3151",
163
"month":"March",
164
"extendedPrice":2246.4,
165
"orderDate":"2018-03-23 00:00:00"
166
},
167
{
168
"country":"USA",
169
"orderNumber":10396,
170
"year":2018,
171
"countrycode":"us",
172
"quantityOrdered":37,
173
"priceEach":90.57,
174
"productName":"1940 Ford Delivery Sedan",
175
"productLine":"Vintage Cars",
176
"sortkey":3,
177
"productCode":"S24_3816",
178
"month":"March",
179
"extendedPrice":3351.09,
180
"orderDate":"2018-03-23 00:00:00"
181
},
182
{
183
"country":"USA",
184
"orderNumber":10396,
185
"year":2018,
186
"countrycode":"us",
187
"quantityOrdered":39,
188
"priceEach":66.67,
189
"productName":"The Schooner Bluenose",
190
"productLine":"Ships",
191
"sortkey":3,
192
"productCode":"S700_1138",
193
"month":"March",
194
"extendedPrice":2600.13,
195
"orderDate":"2018-03-23 00:00:00"
196
},
197
{
198
"country":"France",
199
"orderNumber":10397,
200
"year":2018,
201
"countrycode":"fr",
202
"quantityOrdered":32,
203
"priceEach":80.55,
204
"productName":"The Mayflower",
205
"productLine":"Ships",
206
"sortkey":3,
207
"productCode":"S700_1938",
208
"month":"March",
209
"extendedPrice":2577.6,
210
"orderDate":"2018-03-28 00:00:00"
211
}
212
]
213
I have created below function but its not working properly
JavaScript
1
29
29
1
var groups = ['country', 'productLine', 'month']; // this can be dynamic
2
var sum = ['priceEach']; // this can be dynamic
3
4
function createGroup (groups, data, sum, childNode) {
5
let [primaryGroup, rest] = groups;
6
7
let groupedData = data.reduce((acc, current) => {
8
let chunk = {
9
'Name': current[primaryGroup],
10
[primaryGroup]: current[primaryGroup],
11
[sum]: data.filter(item => item[primaryGroup] === current[primaryGroup])
12
.map(el => el[sum])
13
.reduce((total, current) => total + current),
14
rest.length > 0 ? {[groups[childNode]]: createGroup(rest, data, sum,childNode+1 )} : {}) (
15
}
16
17
acc.push(chunk)
18
return acc
19
}, [])
20
.reduce((acc, current) => {
21
const x = acc.find(item => item[primaryGroup] === current[primaryGroup])
22
return !x ? acc.concat([current]) : acc
23
}, [])
24
25
return groupedData;
26
}
27
28
const tree = createGroup(groups,data,sum, 1);
29
Required below sample result (I haven’t included other country but I need all)
JavaScript
1
73
73
1
[
2
{
3
"country":[
4
{
5
"Name":"Spain",
6
"priceEach":60.28,
7
"productline":[
8
{
9
"Name":"Trains",
10
"priceEach":60.28,
11
"month":[
12
{
13
"Name":"March",
14
"priceEach":60.28
15
}
16
]
17
}
18
]
19
}
20
]
21
},
22
{
23
"country":[
24
{
25
"Name":"France",
26
"priceEach":578.25,
27
"productline":[
28
{
29
"Name":"Classic Cars",
30
"priceEach":174.45,
31
"month":[
32
{
33
"Name":"March",
34
"priceEach":174.45
35
}
36
]
37
},
38
{
39
"Name":"Trucks and Buses",
40
"priceEach":123.76,
41
"month":[
42
{
43
"Name":"March",
44
"priceEach":123.76
45
}
46
]
47
},
48
{
49
"Name":"Trains",
50
"priceEach":199.49,
51
"month":[
52
{
53
"Name":"April",
54
"priceEach":199.49
55
}
56
]
57
},
58
{
59
"Name":"Ships",
60
"priceEach":80.55,
61
"month":[
62
{
63
"Name":"June",
64
"priceEach":80.55
65
}
66
]
67
}
68
]
69
}
70
]
71
}
72
]
73
Advertisement
Answer
Well, it is probably hangs
because of O(n log n)
(you iterate the same data
over and over).
Note: priceEach
is static in this solution, but you can redesign the recursive function
JavaScript
1
67
67
1
function createGroup(groups, i, data, parent){
2
3
//Take current group
4
var group = groups[i];
5
var nGroup = groups[i+1];
6
7
// No more group, stop recursivity
8
if (!group) return parent;
9
10
// Now, let's reduce by current group
11
//FIX: We need to add an empty item to force the reduce method
12
if(data.length == 1) {
13
data.push({priceEach:0});
14
}
15
16
var root = data.reduce((prev, next) => {
17
//First time, add prev value
18
if(parent.length == 0) {
19
20
var item = {};
21
item[group] = {
22
"Name": prev[group],
23
"priceEach": prev.priceEach
24
}
25
//Next group?
26
if(nGroup){
27
item[group][nGroup] = createGroup(groups, i+1, [prev], []);
28
}
29
30
parent.push(item);
31
}
32
33
//Check if we need to reduce
34
for(var item of parent){
35
if(item[group].Name == next[group]) {
36
item[group].priceEach += next.priceEach;
37
//Next group?
38
if(nGroup){
39
item[group][nGroup] = createGroup(groups, i+1, [prev, next], item[group][nGroup]);
40
}
41
return parent;
42
}
43
}
44
45
//We did not reduce, so add next as item
46
if(next[group]) {
47
var item = {};
48
item[group] = {
49
"Name": next[group],
50
"priceEach": next.priceEach
51
}
52
//Next group?
53
if(nGroup){
54
item[group][nGroup] = createGroup(groups, i+1, [next], []);
55
}
56
57
parent.push(item);
58
}
59
60
return parent;
61
});
62
63
return root;
64
}
65
66
createGroup(["country", "productLine", "month"], 0, data, []);
67
If country, productLine, month
are static, you can run the classic way