let a = ["CBSE/X-White","HOS/A/A1","FoodHOS/S1","CBSE/X-Green","HOS/A/A2","FoodHOS/S1","CBSE/IX-White","HOS/B/B1","FoodHOS/S1","TRP/T1"]
I am trying to convert above value of
"a" to below OBJECT we can observe that it was Unique values also.
I output Should be like this After Converting
{
"CBSE":[
"X-WHITE",
"X-Green",
"IX-White"
],
"HOS":{
"A":[
"A1",
"A2"
],
"B":[
"B1"
]
},
"FoodHOS":[
"S1",
"S2"
],
"TRP":[
"T1"
]
}
What I have tried:
I am Converted the
"a" in to the Below Form with this CODE
const munge = a =>
a.reduce((res, e) => {
e = e.split("/");
let a = {};
let previousKey = e.shift();
res[previousKey] = a;
let root = res;
while (e.length) {
const newKey = e.shift();
if(e.length == 0){
let b = [];
b.push(newKey);
root[previousKey] = b;
}
else
a[newKey] = {};
if (e.length) {
root = a;
a= a[newKey];
}
previousKey = newKey;
}
return res;
}, {});
console.log(JSON.stringify(munge(a)));
{
"CBSE":[
"IX-White"
],
"HOS":{
"B":[
"B1"
]
},
"FoodHOS":[
"S1"
],
"TRP":[
"T1"
]
}
I think i got all the Last Values
Can Any One Help me out ?