【问题标题】:merge nodes with the same key合并具有相同键的节点
【发布时间】:2018-01-19 05:40:13
【问题描述】:

我有一个可能有相同键的 Treenode..我需要合并它们,这样就没有重复的文件夹..

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

我需要合并硼化物,使钛和锆在硼化物之下。 硼化物将成为钛和锆的母体。

结构是

硼化物-

  • 钛 - B-102.json
  • 锆 - B103.json

这是我的代码..

 var arr = JSON.stringify(parentObj);
  var result = [];

  for (var i = 0; i < arr.length; i++) {
  console.log("inside i loop");

  var found = false;
  for (var j = 0; j < result.length; j++) {
    console.log("inside i loop");
    if (result[j].children.filePath == arr[i].children.filePath) {
      found = true;
      result[j].nodes = result[j].nodes.concat(arr[i].nodes);
      break;
    }
  }
  if (!found) {
    result.push(arr[i]);

  }
}

感谢您的帮助

更新:如果我在第二级有相同的钛文件夹,文件夹会重复(更新 JSON)..请告知如何解决这个问题..

【问题讨论】:

  • 注意:为什么JSON.parse(JSON.stringify(parentObj))
  • parentObj 是一个treenode..所以我试图转换成字符串,然后再转换成数组
  • 只有顶层可能有重复吗?
  • 你指定ParentObj的方式(首字母大写:代码故障的第一个原因),它已经是一个数组了。
  • 现在..只有顶层有重复

标签: javascript arrays typescript


【解决方案1】:

一种常见且有效的方法是使用公共属性值作为键,完整对象作为值来创建一个 hashmap 对象

然后是一个简单的查找来查看该键是否已经存在。如果是这样,请将当前与 hashmap 中的合并。然后最终从 hashmap 中获取合并结果

const tmp ={}

ParentObj.children.forEach((o) => {
  const path = o.data.filePath;
  if (tmp[path]) {
    tmp[path].children = tmp[path].children || [];// in case no children property or array exists
    tmp[path].children.push(...o.children)
  } else {
    tmp[path] = o;
  }
  
});

ParentObj.children = Object.values(tmp);
console.log(ParentObj)
.as-console-wrapper {	max-height: 100%!important;}
<script>
  const 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": "zirconium"
        },
        "children": [{
          "data": {
            "filePath": "srd13_B-103.json"
          },
          "children": []
        }]
      }]
    }]
  }
</script>

【讨论】:

  • 你成就了我的一天!!!非常感谢..如果有任何问题我会回复..到目前为止工作良好..
  • Object.values(tmp) 是否有任何替代方法,我正在使用 typescript 并收到错误“错误 TS2339: Property 'values' does not exist on type 'ObjectConstructor'。”.. 我试过了在 tsconfig.json 中使用 ES2017.. 但仍然出现错误
  • 可以使用for in 循环。 const arr=[]; for(let key in tmp){arr.push(tmp[key])}
  • 你好查理,对不起..第二级也有重复..硼化物-钛-102.json,硼化物-钛-103.json
  • 如何重复使用您的脚本来检查多个级别的重复项?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多