Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)));
//--------------------------------------------------------------
//What i got the result is below

   {
   "CBSE":[
      "IX-White"
   ],
   "HOS":{
      "B":[
         "B1"
      ]
   },
   "FoodHOS":[
      "S1"
   ],
   "TRP":[
      "T1"
   ]
}

I think i got all the Last Values

Can Any One Help me out ?
Posted
Updated 9-Aug-18 1:17am
v4

1 solution

Hi try this code. It is not very dynamic, but see if it gives you some idea

let obj = {};
   let arrtemp = {};

   a.forEach(function (item) {
       arr = item.split("/");
       let temp = {};
       let previousKey = arr.shift();

       if (!(obj ? hasOwnProperty.call(obj, previousKey) : false)) {
           obj[previousKey] = temp;
           let root = obj;
           while (arr.length) {
               const newKey = arr.shift();

               if (arr.length == 0) {
                   let b = [];
                   b.push(newKey);
                   root[previousKey] = b;
               }
               else
                   temp[newKey] = {};

               if (arr.length) {
                   root = temp;
                   temp = temp[newKey];
               }
               previousKey = newKey;
           }
           arrtemp = obj;
       }
       else {
           const newKey = arr.shift();
           if (arr.length == 0 && obj[previousKey].indexOf(newKey) < 0) {
               obj[previousKey].push(newKey);
           }
           else {
               obj[previousKey][newKey] = {};

               if (arr.length) {
                   let b = [];
                   b.push(arr.shift());
                   obj[previousKey][newKey] = b;
               }
           }

       }
   });

   console.log(JSON.stringify(arrtemp));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900