将数组转换为修改后的对象 [英] Convert an array in to an modified object

查看:91
本文介绍了将数组转换为修改后的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"]



我试图将 a 的上述值转换​​为以下OBJECT,我们可以观察到它也是唯一值。



我的输出应该像这样转换后


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"
   ]
}





我尝试了什么:



我使用此代码将 a 转换为以下表格





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 ?

推荐答案

您好试试这个代码。这不是很有活力,但看看它是否给你一些想法



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));


这篇关于将数组转换为修改后的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆