Repository navigation
Expand file tree
/
Copy pathflat-array-to-array-of-nested-objects.js
More file actions
61 lines (50 loc) · 1.82 KB
/
Copy pathflat-array-to-array-of-nested-objects.js
File metadata and controls
61 lines (50 loc) · 1.82 KB
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
const flatArrayToNested = (function () {
const _store = {
processed: [],
endResult: [],
parentField: '',
groupingField: ''
};
function _pushDistinct(arr, item) {
if (arr.indexOf(item) === -1) {arr.push(item);}
}
function _getChildItems(inArray, currentItem) {
let items = inArray.filter(x => x[_store.groupingField] === currentItem[_store.parentField] && x[_store.groupingField] !== x[_store.parentField]);
_pushDistinct(_store.processed, currentItem[_store.parentField]);
if (items.length === 0) {
_pushDistinct(_store.endResult, currentItem);
return;
}
for (let i = 0; i < items.length; i++) {
_pushDistinct(_store.processed, items[i][_store.parentField]);
}
currentItem.Children = items;
_pushDistinct(_store.endResult, currentItem);
_getChildItems(items, items[0]);
}
function convert(arr, parentField, groupingField) {
_store.parentField = parentField;
_store.groupingField = groupingField;
for (let i = 0; i < arr.length; i++) {
let currentItem = arr[i];
_getChildItems(arr, currentItem);
}
let remove = _store.endResult
.filter(value => _store.processed.includes(value[_store.groupingField]) && !(value[_store.groupingField] === value[_store.parentField]));
for (let i = 0; i < remove.length; i++) {
let currentItem = remove[i];
let idx = _store.endResult.indexOf(currentItem);
if( idx !== -1){
_store.endResult.splice(idx, 1);
}
}
return _store.endResult;
}
return {
convert: convert,
};
})();
// noinspection JSUnresolvedVariable
module.exports = {
array: flatArrayToNested
};