-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.js
69 lines (55 loc) · 2.06 KB
/
sort.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const sorter = {};
sorter.count = (input) => {
const aggregatedResults = {};
const workdata = input.lectures;
for (let i = 0; i < workdata.length; i += 1) {
const { results } = workdata[i].attributes;
if (typeof aggregatedResults[i] === 'undefined') {
aggregatedResults[i] = {};
}
for (let k = 0; k < results.length; k += 1) {
const materialId = results[k].material_id;
const modelType = results[k].model_type[0];
const resultWeight = results[k].weight[0];
if (typeof aggregatedResults[i][materialId] === 'undefined') {
aggregatedResults[i][materialId] = {};
}
if (typeof aggregatedResults[i][materialId].modelTypes === 'undefined') {
aggregatedResults[i][materialId][modelType] = resultWeight;
}
}
}
return aggregatedResults;
};
sorter.sort = (input) => {
const sortedResults = {};
console.log('\nstart sorting...');
for (let i = 0; i < input.lectures.length; i += 1) {
const { results } = input.lectures[i].attributes;
sortedResults[i] = input.lectures[i];
sortedResults[i].attributes.results = [];
for (let k = 0; k < results.length; k += 1) {
const materialId = results[k].material_id;
const modelType = results[k].model_type[0];
const resultWeight = results[k].weight[0];
const result = results[k];
let matchFound = false;
sortedResults[i].attributes.results.forEach((item) => {
if (item.material_id === materialId) matchFound = true;
});
if (!matchFound) {
sortedResults[i].attributes.results.push(result);
} else {
console.log(`lecture ${i} matching result found at id ${materialId}`);
const matchIndex = sortedResults[i].attributes.results.findIndex(
(item) => item.material_id === materialId,
);
sortedResults[i].attributes.results[matchIndex].model_type.push(modelType);
sortedResults[i].attributes.results[matchIndex].weight.push(resultWeight);
}
}
}
console.log('sorting complete\n');
return sortedResults;
};
module.exports = sorter;