Here is an example of input JSON in the specified format.
var inputData = [{
"TestScenario": "test1",
"Application": "application1",
"Market": "M1"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M2"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M3"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M4"
}, {
"TestScenario": "test2",
"Application": "application2",
"Market": "M5"
}, {
"TestScenario": "test2",
"Application": "application3",
"Market": "M5"
}];
The desired tree structure should be as follows.
var outputData = [{
"test1": {
"application1": ["M1", "M2", "M3", "M4"]
}
}, {
"test2": {
"application2": "M5",
"application3": "M5"
}
}];
Approach attempted:
I was successful in generating the tree structure for a single type of TestScenario but faced challenges when dealing with multiple types.
Successful code implementation for one TestScenario:
var dataEntries= [{
"TestScenario": "test1",
"Application": "application1",
"Market": "M1"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M2"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M3"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M4"
}];
var testCases = [];
for (const entry of dataEntries) {
if(testCases.indexOf(entry.TestScenario) === -1) {
testCases.push(entry.TestScenario);
}
}
var marketArray = [];
var finalOutput = [];
var obj = {};
for (const b of dataEntries) {
for (const c of testCases) {
if (b.TestScenario === c) {
obj[c] = {};
obj[c][b.Application] = [];
}
if (finalOutput.indexOf(obj) === -1) {
finalOutput.push(obj);
}
}
for (const c of finalOutput) {
var arr1 = Object.keys(c);
for (const d of arr1) {
if (b.TestScenario === d) {
var arr2 = Object.keys(c[d]);
for (const e of arr2) {
if(b.Application === e) {
marketArray.push(b.Market);
c[d][e] = marketArray;
}
}
}
}
}
}
console.log('Final Output', finalOutput);
Challenges encountered with multiple TestScenarios:
var dataEntries= [{
"TestScenario": "test1",
"Application": "application1",
"Market": "M1"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M2"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M3"
}, {
"TestScenario": "test1",
"Application": "application1",
"Market": "M4"
}, {
"TestScenario": "test2",
"Application": "application2",
"Market": "M5"
}, {
"TestScenario": "test2",
"Application": "application3",
"Market": "M5"
}];
var testCases = [];
for (const entry of dataEntries) {
if(testCases.indexOf(entry.TestScenario) === -1) {
testCases.push(entry.TestScenario);
}
}
var marketArray = [];
var finalOutput = [];
var obj = {};
for (const b of dataEntries) {
for (const c of testCases) {
if (b.TestScenario === c) {
obj[c] = {};
obj[c][b.Application] = [];
}
if (finalOutput.indexOf(obj) === -1) {
finalOutput.push(obj);
}
}
for (const c of finalOutput) {
var arr1 = Object.keys(c);
for (const d of arr1) {
if (b.TestScenario === d) {
var arr2 = Object.keys(c[d]);
for (const e of arr2) {
if(b.Application === e) {
marketArray.push(b.Market);
c[d][e] = marketArray;
}
}
}
}
}
}
console.log('Final Output', finalOutput);