{"id":53590,"date":"2024-12-03T17:47:03","date_gmt":"2024-12-03T09:47:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53590\/"},"modified":"2024-12-03T17:47:03","modified_gmt":"2024-12-03T09:47:03","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e6%89%81%e5%b9%b3%e5%8c%96%e7%9a%84%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e6%88%90%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53590\/","title":{"rendered":"\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733121211674d54bb2fa97.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6241\u5e73\u5316\u6811\u5f62\u6570\u7ec4\u8f6c\u6362<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a\u6811\u5f62\u6570\u7ec4\u7ed3\u6784\uff1f<\/p>\n<p><strong>\u539f\u59cb\u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>const arr = [  \n  {id: 4, pid: 3},  \n  {id: 'aa',pid:'a'},\n  {id: 1, pid: null},  \n  {id: 3, pid: 2},\n  {id: 'a',pid: 'a0'},\n  {id: 2, pid: 1},\n  {id: 'a0',pid: null}  \n];  <\/pre>\n<p><strong>\u671f\u671b\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>[\n    {\n        \"id\": 1,\n        \"pid\": null,\n        \"children\": [\n            {\n                \"id\": 2,\n                \"pid\": 1,\n                \"children\": [\n                    {\n                        \"id\": 3,\n                        \"pid\": 2,\n                        \"children\": [\n                            {\n                                \"id\": 4,\n                                \"pid\": 3\n                            }\n                        ]\n                    }\n                ]\n            }\n        ]\n    },\n    {\n        \"id\": \"a0\",\n        \"pid\": null,\n        \"children\": [\n            {\n                \"id\": \"a\",\n                \"pid\": \"a0\",\n                \"children\": [\n                    {\n                        \"id\": \"aa\",\n                        \"pid\": \"a\"\n                    }\n                ]\n            }\n        ]\n    }\n]<\/pre>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/strong><\/p>\n<pre>arr.reduce((o, i) =&gt; {\n    i = object.assign(o[i.id] ??= {}, i);\n    ((o[i.pid ?? ''] ??= {}).children ??= []).push(i);\n    return o;\n}, {})['']?.children<\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>[\n  {\n    \"id\": 1,\n    \"pid\": null,\n    \"children\": [\n      {\n        \"id\": 2,\n        \"pid\": 1,\n        \"children\": [\n          {\n            \"id\": 3,\n            \"pid\": 2,\n            \"children\": [\n              {\n                \"id\": 4,\n                \"pid\": 3\n              }\n            ]\n          }\n        ]\n      }\n    ]\n  },\n  {\n    \"id\": \"a0\",\n    \"pid\": null,\n    \"children\": [\n      {\n        \"id\": \"a\",\n        \"pid\": \"a0\",\n        \"children\": [\n          {\n            \"id\": \"aa\",\n            \"pid\": \"a\"\n          }\n        ]\n      }\n    ]\n  }\n]<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528 CSS \u5b9e\u73b0\u5144\u5f1f\u5143\u7d20\u5bbd\u5ea6\u8ddf\u968f\u6700\u957f\u7684\u4e00\u4e2a\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u6210\u6811\u5f62\u7ed3\u6784\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 \u6241\u5e73\u5316\u6811\u5f62\u6570\u7ec4\u8f6c\u6362 \u95ee\u9898\uff1a \u5982\u4f55\u5c06\u6241\u5e73\u5316\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a\u6811\u5f62\u6570\u7ec4\u7ed3\u6784\uff1f \u539f\u59cb\u6570\u7ec4\uff1a const arr = [ {id: 4, pid: 3}, {id: &#8216;aa&#8217;,pid:&#8217;a&#8217;}, {id: 1, pid: null}, {id: 3, pid: 2}, {id: &#8216;a&#8217;,pid: &#8216;a0&#8217;}, {id: 2, pid: 1}, {id: &#8216;a0&#8217;,pid: null} ]; \u671f\u671b\u7ed3\u679c\uff1a [ { &#8220;id&#8221;: 1, &#8220;pid&#8221;: null, &#8220;children&#8221;: [ { &#8220;id&#8221;: 2, &#8220;pid&#8221;: 1, &#8220;children&#8221;: [ { &#8220;id&#8221;: 3, &#8220;pid&#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-53590","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53590","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=53590"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53590\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}