合并子项中的重复文件路径 - 树节点

时间:2018-01-28 04:05:24

标签: javascript arrays typescript ecmascript-6

我正在使用以下json构建树结构..如果你注意到,有2个子文件具有相同的文件路径borides和titanium,我需要合并它们以便不创建重复的文件夹。 我的输出文件夹结构是

SRD 13 
    - borides
        - titanium
            - srd13_B-102.json
            - srd13_B-103.json

以下json,硼化物和钛正在重复

输入json

parentObj = 

[{
"data": {
    "resTitle": "-JANAF Thermochemical Tables - SRD 13"
},
"children": [{
    "data": {
        "filePath": "borides"
    },
    "children": [{
        "data": {
            "filePath": "titanium"
        },
        "children": [{
            "data": {
                "filePath": "srd13_B-102.json"
            },
            "children": []
        }]
    }]
}, {
    "data": {
        "filePath": "borides"
    },
    "children": [{
        "data": {
            "filePath": "titanium"
        },
        "children": [{
            "data": {
                "filePath": "srd13_B-103.json"
            },
            "children": []
        }]
    }]
}]
}]

输出json将是

[{
"data": {
    "resTitle": "-JANAF Thermochemical Tables - SRD 13"
},
"children": [{
    "data": {
        "filePath": "borides"
    },
    "children": [{
        "data": {
            "filePath": "titanium"
        },
        "children": [{
                "data": {
                    "filePath": "srd13_B-102.json"
                }
            },
            {
                "data": {
                    "filePath": "srd13_B-103.json"
                }
            }
        ]
    }]
}]
}]

我使用以下脚本来合并节点,但它只在第一级查找相同的文件路径,但在这种情况下,第二级也有相同的文件路径..

const tmp ={}
ParentObj.children.forEach((o) => {
    const path = o.data.filePath;
    if (tmp[path]) {
        tmp[path].children = tmp[path].children || [];
        tmp[path].children.push(...o.children)
    } else {
        tmp[path] = o;
    }
});
ParentObj.children = Object.values(tmp);

我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你需要一些递归来实现这一目标。我使用Array.prototype.reduce()和递归函数merge来合并重复的子节点:

const merge = (data) => {
  return data.reduce((result, current) => {
      let dup = result.find(d => d.data.filePath === current.data.filePath);
      if (!dup) {
          result.push(current.children && current.children.length > 0 ? {
              data: current.data,
              children: merge(current.children)
          } : {data: current.data});
      } else if (current.children && current.children.length > 0) {
          dup.children = merge(dup.children.concat(current.children));
      }
      return result;
  }, []);
};


let parentObj = 
[
   {
      "data":{
         "resTitle":"-JANAF Thermochemical Tables - SRD 13"
      },
      "children":[
         {
            "data":{
               "filePath":"borides"
            },
            "children":[
               {
                  "data":{
                     "filePath":"titanium"
                  },
                  "children":[
                     {
                        "data":{
                           "filePath":"srd13_B-102.json"
                        },
                        "children":[

                        ]
                     }
                  ]
               }
            ]
         },
         {
            "data":{
               "filePath":"borides"
            },
            "children":[
               {
                  "data":{
                     "filePath":"titanium"
                  },
                  "children":[
                     {
                        "data":{
                           "filePath":"srd13_B-103.json"
                        },
                        "children":[

                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
];

console.log(JSON.stringify(merge(parentObj), null, '  '));