Looking for a solution to merge Treernode keys that might be the same in order to eliminate duplicate folders.
ParentObj = [{
"data": {
"resTitle": "-JANAF Thermochemical Tables - SRD 13"
},
"children": [{
"data": {
"filePath": "borides"
},
"children": [{
"data": {
"filePath": "titanium"
},
"children": [{
"data": {
"filePath": "srd13_B-102.json"
},
"children": []
}]
}]
}, {
"data": {
"filePath": "borides"
},
"children": [{
"data": {
"filePath": "titanium"
},
"children": [{
"data": {
"filePath": "srd13_B-103.json"
},
"children": []
}]
}]
}]
}]
The goal is to consolidate 'borides' so that 'titanium' and 'zirconium' are nested under 'borides'. 'Borides' will act as the parent for 'titanium' and 'zirconium'.
The desired structure would look like this:
borides -
- titanium - B-102.json
- zirconium - B103.json
Provided below is the code snippet currently being utilized:
var arr = JSON.stringify(parentObj);
var result = [];
for (var i = 0; i < arr.length; i++) {
console.log("inside i loop");
var found = false;
for (var j = 0; j < result.length; j++) {
console.log("inside i loop");
if (result[j].children.filePath == arr[i].children.filePath) {
found = true;
result[j].nodes = result[j].nodes.concat(arr[i].nodes);
break;
}
}
if (!found) {
result.push(arr[i]);
}
}
Your assistance is greatly appreciated.
Update: An issue arises when there are duplicate 'titanium' folders at the second level, causing folder repetition. See updated JSON data. Any advice on how to address this?