{"id":3879,"date":"2024-11-10T16:28:19","date_gmt":"2024-11-10T08:28:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3879\/"},"modified":"2024-11-10T16:28:19","modified_gmt":"2024-11-10T08:28:19","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e7%bb%b4%e5%b5%8c%e5%a5%97%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%8c%85%e5%90%ab%e5%ad%90%e7%ba%a7%e5%b1%9e%e6%80%a7%e7%9a%84%e6%a0%91%e7%8a%b6%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3879\/","title":{"rendered":"\u5982\u4f55\u5c06\u4e00\u7ef4\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5305\u542b\u5b50\u7ea7\u5c5e\u6027\u7684\u6811\u72b6\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173115757244444.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u4e00\u7ef4\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5305\u542b\u5b50\u7ea7\u5c5e\u6027\u7684\u6811\u72b6\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u4e00\u7ef4\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5305\u542b\u5b50\u7ea7\u5c5e\u6027\u7684\u6811\u72b6\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5177\u6709\u5b50\u7ea7\u5c5e\u6027\u7684\u6570\u636e\u683c\u5f0f<\/strong><\/p>\n<p>\u672c\u6587\u5c06\u6f14\u793a\u5982\u4f55\u5c06\u7ed9\u5b9a\u7684\u4e00\u7ef4\u5d4c\u5957\u6570\u7ec4\uff1a<\/p>\n<pre>[\n  {\n    \"id\": 1,\n    \"key\": \"\",\n    \"name\": \"\u7cfb\u7edf\u7ba1\u7406\",\n    \"level\": 1,\n    \"type\": 1,\n    \"icon\": \"\",\n    \"code\": \"a001\",\n    \"parentcode\": \"\",\n    \"num\": \"a-1\",\n  },\n  {\n    \"id\": 7,\n    \"key\": \"\",\n    \"name\": \"\u65e5\u5fd7\u7ba1\u7406\",\n    \"level\": 1,\n    \"type\": 1,\n    \"icon\": \"\",\n    \"code\": \"a004\",\n    \"parentcode\": \"\",\n    \"num\": \"a-7\",\n  },\n  {\n    \"id\": 8,\n    \"key\": \"roleadd\",\n    \"name\": \"\u89d2\u8272\u7ba1\u7406\",\n    \"level\": 2,\n    \"type\": 1,\n    \"icon\": \"\",\n    \"code\": \"b001\",\n    \"parentcode\": \"a001\",\n    \"num\": \"1-1\",\n  },\n  {\n    \"id\": 14,\n    \"key\": \"logomanage\",\n    \"name\": \"\u65e5\u5fd7\u7ba1\u7406\",\n    \"level\": 2,\n    \"type\": 1,\n    \"icon\": null,\n    \"code\": \"e001\",\n    \"parentcode\": \"a004\",\n    \"num\": \"1-1\",\n  },\n]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8f6c\u6362\u4e3a\u5305\u542b\u5b50\u7ea7\u5c5e\u6027\u7684\u6811\u72b6\u6570\u636e\uff1a<\/p>\n<pre>[\n  {\n    \"id\": 1,\n    \"key\": null,\n    \"name\": \"\u7cfb\u7edf\u7ba1\u7406\",\n    \"level\": 1,\n    \"icon\": null,\n    \"num\": \"a-1\",\n    \"children\": [\n      {\n        \"id\": 8,\n        \"key\": \"roleadd\",\n        \"name\": \"\u89d2\u8272\u7ba1\u7406\",\n        \"level\": 2,\n        \"type\": 1,\n        \"icon\": null,\n        \"code\": \"b001\",\n        \"parentcode\": \"a001\",\n        \"num\": \"1-1\",\n     }\n   ]\n  },\n  {\n    \"id\": 7,\n    \"key\": null,\n    \"name\": \"\u65e5\u5fd7\u7ba1\u7406\",\n    \"level\": 1,\n    \"type\": 1,\n    \"icon\": null,\n    \"num\": \"a-7\",\n    \"children\": [\n      {\n        \"id\": 14,\n        \"key\": \"logomanage\",\n        \"name\": \"\u65e5\u5fd7\u7ba1\u7406\",\n        \"level\": 2,\n        \"type\": 1,\n        \"icon\": null,\n        \"code\": \"e001\",\n        \"parentcode\": \"a004\",\n        \"num\": \"1-1\",\n      }\n    ]\n  },\n]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u5bf9\u8c61\u6620\u5c04\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u7528 parentcode \u4f5c\u4e3a\u952e\uff0c\u5c06\u6bcf\u4e2a\u5b50\u7ea7\u5143\u7d20\u6620\u5c04\u5230\u5176\u7236\u7ea7\u5143\u7d20\u3002<\/li>\n<li> <strong>\u904d\u5386\u6570\u7ec4\uff1a<\/strong>\u904d\u5386\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<li> <strong>\u68c0\u67e5\u6620\u5c04\uff1a<\/strong>\u67e5\u770b\u6620\u5c04\u4e2d\u662f\u5426\u5b58\u5728\u8be5\u5143\u7d20\u7684 parentcode\u3002<\/li>\n<li> <strong>\u5982\u679c\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u5b69\u5b50\uff1a<\/strong>\u5982\u679c\u5b58\u5728\uff0c\u5219\u5c06\u8be5\u5143\u7d20\u4f5c\u4e3a\u5b50\u7ea7\u6dfb\u52a0\u5230\u5176\u7236\u7ea7\u7684 children \u6570\u7ec4\u4e2d\u3002<\/li>\n<li> <strong>\u5426\u5219\uff0c\u521b\u5efa\u6839\u5143\u7d20\uff1a<\/strong>\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u65b0\u7684\u6839\u5143\u7d20\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u6570\u7ec4\u4e2d\u5e76\u5c06\u8be5\u5143\u7d20\u4f5c\u4e3a\u5176\u5b50\u7ea7\u6dfb\u52a0\u5230 children \u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ol>\n<p><strong>javascript \u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>const data = [\n  {\n    \"id\": 1,\n    \"key\": \"\",\n    \"name\": \"\u7cfb\u7edf\u7ba1\u7406\",\n    \"level\": 1,\n    \"icon\": \"\",\n    \"code\": \"A001\",\n    \"parentCode\": \"\",\n    \"num\": \"A-1\",\n  },\n  \/\/ ...\n];\n\nconst parentMap = {};\n\ndata.forEach((item) =&gt; {\n  parentMap[item.parentCode] = parentMap[item.parentCode] || [];\n  parentMap[item.parentCode].push(item);\n});\n\nconst result = [];\n\ndata.forEach((item) =&gt; {\n  if (parentMap[item.code]) {\n    parentMap[item.parentCode].forEach((child) =&gt; {\n      item.children = item.children || [];\n      item.children.push(child);\n    });\n  } else {\n    result.push(item);\n  }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u5177\u6709\u5b50\u7ea7\u5c5e\u6027\u7684\u6811\u72b6\u6570\u636e\u7ed3\u6784\uff0c\u6309\u7167\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u5c42\u6b21\u7ed3\u6784\u7ec4\u7ec7\u5143\u7d20\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u4e00\u7ef4\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5305\u542b\u5b50\u7ea7\u5c5e\u6027\u7684\u6811\u72b6\u6570\u636e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5177\u6709\u5b50\u7ea7\u5c5e\u6027\u7684\u6570\u636e\u683c\u5f0f \u672c\u6587\u5c06\u6f14\u793a\u5982\u4f55\u5c06\u7ed9\u5b9a\u7684\u4e00\u7ef4\u5d4c\u5957\u6570\u7ec4\uff1a [ { &#8220;id&#8221;: 1, &#8220;key&#8221;: &#8220;&#8221;, &#8220;name&#8221;: &#8220;\u7cfb\u7edf\u7ba1\u7406&#8221;, &#8220;level&#8221;: 1, &#8220;type&#8221;: 1, &#8220;icon&#8221;: &#8220;&#8221;, &#8220;code&#8221;: &#8220;a001&#8221;, &#8220;parentcode&#8221;: &#8220;&#8221;, &#8220;num&#8221;: &#8220;a-1&#8221;, }, { &#8220;id&#8221;: 7, &#8220;key&#8221;: &#8220;&#8221;, &#8220;name&#8221;: &#8220;\u65e5\u5fd7\u7ba1\u7406&#8221;, &#8220;level&#8221;: 1, &#8220;type&#8221;: 1, &#8220;icon&#8221;: &#8220;&#8221;, &#8220;code&#8221;: &#8220;a004&#8221;, &#8220;parentcode&#8221;: &#8220;&#8221;, &#8220;num&#8221;: &#8220;a-7&#8221;, }, { &#8220;id&#8221;: 8, &#8220;key&#8221;: &#8220;roleadd&#8221;, &#8220;name&#8221;: &#8220;\u89d2\u8272\u7ba1\u7406&#8221;, &#8220;level&#8221;: 2, &#8220;type&#8221;: 1, &#8220;icon&#8221;: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3879","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3879"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3879\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}