{"id":3667,"date":"2024-11-10T15:23:53","date_gmt":"2024-11-10T07:23:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3667\/"},"modified":"2024-11-10T15:23:53","modified_gmt":"2024-11-10T07:23:53","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e6%89%81%e5%b9%b3%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%b5%8c%e5%a5%97%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3667\/","title":{"rendered":"\u5982\u4f55\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111510399789.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js\u6570\u636e\u6574\u7406\uff1a\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784<\/strong><\/p>\n<p>\u8981\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\uff0c\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u54c8\u5e0c\u8868\uff1a<\/strong>\u5c06\u6574\u7406\u540e\u7684\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u54c8\u5e0c\u8868\u4e2d\uff0c\u5176\u4e2d\u952e\u662f\u7236\u7ea7\u7f16\u53f7\uff0c\u503c\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b58\u50a8\u8be5\u7236\u7ea7\u7f16\u53f7\u4e0b\u7684\u6240\u6709\u5b50\u7ea7\u3002<\/li>\n<li> <strong>\u904d\u5386\u6570\u636e\uff1a<\/strong>\u4f9d\u6b21\u904d\u5386\u6241\u5e73\u6570\u636e\u4e2d\u7684\u6bcf\u4e00\u9879\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u54c8\u5e0c\u8868\u4e2d\u3002<\/li>\n<li> <strong>\u9012\u5f52\u904d\u5386\uff1a<\/strong>\u5bf9\u4e8e\u54c8\u5e0c\u8868\u4e2d\u7f16\u53f7\u4e3a\u5f53\u524d\u9879\u7236\u7ea7\u7f16\u53f7\u7684\u6570\u7ec4\uff0c\u9012\u5f52\u904d\u5386\u8be5\u6570\u7ec4\uff0c\u5c06\u5b50\u7ea7\u6dfb\u52a0\u5230\u5f53\u524d\u9879\u7684\u5b50\u7ea7\u4e2d\u3002<\/li>\n<li> <strong>\u8fd4\u56de\u7ed3\u679c\uff1a<\/strong>\u6700\u540e\uff0c\u8fd4\u56de\u54c8\u5e0c\u8868\u4e2d\u952e\u4e3a\u9876\u7ea7\u7f16\u53f7\u7684\u6570\u7ec4\uff0c\u5373\u8f6c\u6362\u540e\u7684\u5d4c\u5957\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/strong><\/p>\n<pre>\/\/ \u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\nconst transform = (data) =&gt; {\n  const hash = {};\n  \/\/ \u521b\u5efa\u54c8\u5e0c\u8868\n  data.forEach((item) =&gt; {\n    hash[item.parentCode] = hash[item.parentCode] || [];\n    hash[item.parentCode].push(item);\n  });\n  \/\/ \u9012\u5f52\u904d\u5386\n  const result = [];\n  data.forEach((item) =&gt; {\n    const children = hash[item.code];\n    if (children) {\n      item.children = transform(children);\n    }\n    result.push(item);\n  });\n  return result;\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\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>js\u6570\u636e\u6574\u7406\uff1a\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784 \u8981\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\uff0c\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u521b\u5efa\u54c8\u5e0c\u8868\uff1a\u5c06\u6574\u7406\u540e\u7684\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u54c8\u5e0c\u8868\u4e2d\uff0c\u5176\u4e2d\u952e\u662f\u7236\u7ea7\u7f16\u53f7\uff0c\u503c\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b58\u50a8\u8be5\u7236\u7ea7\u7f16\u53f7\u4e0b\u7684\u6240\u6709\u5b50\u7ea7\u3002 \u904d\u5386\u6570\u636e\uff1a\u4f9d\u6b21\u904d\u5386\u6241\u5e73\u6570\u636e\u4e2d\u7684\u6bcf\u4e00\u9879\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u54c8\u5e0c\u8868\u4e2d\u3002 \u9012\u5f52\u904d\u5386\uff1a\u5bf9\u4e8e\u54c8\u5e0c\u8868\u4e2d\u7f16\u53f7\u4e3a\u5f53\u524d\u9879\u7236\u7ea7\u7f16\u53f7\u7684\u6570\u7ec4\uff0c\u9012\u5f52\u904d\u5386\u8be5\u6570\u7ec4\uff0c\u5c06\u5b50\u7ea7\u6dfb\u52a0\u5230\u5f53\u524d\u9879\u7684\u5b50\u7ea7\u4e2d\u3002 \u8fd4\u56de\u7ed3\u679c\uff1a\u6700\u540e\uff0c\u8fd4\u56de\u54c8\u5e0c\u8868\u4e2d\u952e\u4e3a\u9876\u7ea7\u7f16\u53f7\u7684\u6570\u7ec4\uff0c\u5373\u8f6c\u6362\u540e\u7684\u5d4c\u5957\u7ed3\u6784\u3002 \u4ee3\u7801\u5b9e\u73b0\uff1a \/\/ \u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784 const transform = (data) =&gt; { const hash = {}; \/\/ \u521b\u5efa\u54c8\u5e0c\u8868 data.forEach((item) =&gt; { hash[item.parentCode] = hash[item.parentCode] || []; hash[item.parentCode].push(item); }); \/\/ \u9012\u5f52\u904d\u5386 const result = []; data.forEach((item) =&gt; { const children = hash[item.code]; if (children) { item.children = transform(children); } result.push(item); }); return result; [&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-3667","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3667","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=3667"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3667\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}