将数组转换为修改后的对象

时间:2018-08-08 11:48:50

标签: javascript arrays typescript

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”的上面的值转换为下面的对象,我们可以观察到它也是唯一的值。

  

转换后的输出应如下所示

 {
    "CBSE":[
    "X-WHITE",
    "X-Green",
    "IX-White"
    ],
    "HOS":{
     "A":[
       "A1",
       "A2"
       ],
     "B":[
      "B1"
     ]
    },
   "FoodHOS":[
     "S1",
     "S2"
    ],
  "TRP":[
    "T1"
    ]
 }

我尝试过的事情:

我使用此CODE将“ a”转换为以下表格

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

我正在尝试制作一个受角树组件支持的对象 我想使用DataStructure转换为angular的Tree Comp数据输入,

https://material.angular.io/components/tree/overview

https://stackblitz.com/angular/jamlvojaqjeg?file=app%2Ftree-checklist-example.ts

2 个答案:

答案 0 :(得分:1)

  

根据角度的材质树组件,此下面的代码将起作用。   我所做的就是修改了您的代码,并添加了对现有对象的检查。

let a = ["CBSE/sdfsf/X-White", "HOS/A/AA/A1", "FoodHOS/S1", "CBSE/X-Green", "HOS/A/A2",
    "FoodHOS/S1", "CBSE/IX-White", "HOS/A/B1", "FoodHOS/S1", "TRP/T1"]

// const isEmpty = (obj) => {
// return Object.keys(obj).length === 0 && obj.constructor === Object;
// }


const munge = a =>
    a.reduce((res, e) => {
        e = e.split("/");
        let a = {};
        let previousKey = e.shift();
        if (res[previousKey]) {
            a = res[previousKey];
        } else {
            res[previousKey] = a;
        }
        let root = res;
        while (e.length) {
            const newKey = e.shift();

            if (e.length == 0) {
                let b = [];
                if (root[previousKey] instanceof Array) {
                    b = root[previousKey];
                } else if (Object.keys(root[previousKey]).length) {
                    b = root[previousKey];
                }
                if (b instanceof Array) {
                    if (!b.includes(newKey))
                        b.push(newKey);
                    root[previousKey] = b;
                } else {
                    root[previousKey][newKey] = null;
                }
            }
            else {
                if (!a[newKey]) {
                    a[newKey] = {};
                }
            }

            if (e.length) {
                root = a;
                a = a[newKey];
            }
            previousKey = newKey;
        }

        return res;
    }, {});

console.log(JSON.stringify(munge(a)));

答案 1 :(得分:1)

您可以拆分字符串,并将其作为值数组的路径。

var array = ["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"],
    result = array.reduce((r, s) => {
        var p = s.split('/'),
            v = p.pop();

        p
            .reduce((o, k, i, a) => o[k] = o[k] || (i + 1 === a.length ? [] : {}), r)
            .push(v);

        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }