{"id":1626,"date":"2024-11-10T13:22:36","date_gmt":"2024-11-10T05:22:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1626\/"},"modified":"2024-11-10T13:22:36","modified_gmt":"2024-11-10T05:22:36","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%b5%8c%e5%a5%97%e5%af%b9%e8%b1%a1%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%b5%8c%e5%a5%97%e7%bb%93%e6%9e%84%e7%9a%84%e6%95%b0%e7%bb%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1626\/","title":{"rendered":"\u5982\u4f55\u5c06\u5d4c\u5957\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173051825576252.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u5d4c\u5957\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u5d4c\u5957\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\u6570\u7ec4<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5d4c\u5957\u7684\u5bf9\u8c61 obj\uff0c\u8981\u6c42\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u7ec4 list\u3002\u6bcf\u4e2a\u6570\u7ec4\u9879\u4ee3\u8868 obj \u4e2d\u7684\u4e00\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u5217\u8868\uff0c\u8f6c\u6362\u4e3a\u4ee5\u4e0b\u683c\u5f0f\uff1a<\/p>\n<pre>const list = [\n  {\n    id: 1, \/\/ \u81ea\u52a8\u751f\u6210\u7684 ID\n    name: 'a', \/\/ \u5bf9\u8c61\u7684\u952e\n    childList: [\n      { id: 2, name: 'a1' }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u9879\n      { id: 3, name: 'a2' }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e8c\u9879\n    ],\n  },\n  {\n    id: 4, \/\/ \u81ea\u52a8\u751f\u6210\u7684 ID\n    name: 'b', \/\/ \u5bf9\u8c61\u7684\u952e\n    childList: [\n      { id: 5, name: 'b1' }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u9879\n      { id: 6, name: 'b2' }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e8c\u9879\n      { id: 7, name: 'b3' }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e09\u9879\n    ],\n  },\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Object.entries() \u548c reduce() \u65b9\u6cd5\u3002<\/p>\n<p>\u5c06 obj \u7684\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>let obj = {\n  \"a\": [\n    \"a1\",\n    \"a2\"\n  ],\n  \"b\": [\n    \"b1\",\n    \"b2\",\n    \"b3\"\n  ]\n};\nlet entries = Object.entries(obj);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528 reduce() \u5c06\u6bcf\u4e2a\u952e\u503c\u5bf9\u8f6c\u6362\u4e3a list \u9879\uff1a<\/p>\n<pre>entries.reduce((acc, [name, val]) =&gt; {\n  acc.push({\n    id: id++,\n    name,\n    childList: val.map(name =&gt; ({ id: id++, name })),\n  });\n  return acc;\n}, []);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>id \u662f\u81ea\u52a8\u751f\u6210\u7684 ID\u3002<\/li>\n<li>name \u662f\u952e\u503c\u5bf9\u7684\u952e\u3002<\/li>\n<li>childList \u662f\u503c\u6570\u7ec4\uff0c\u6bcf\u4e2a\u503c\u5df2\u8f6c\u6362\u4e3a\u5177\u6709\u81ea\u52a8\u751f\u6210 ID \u548c\u503c\u7684\u5b50\u9879\u3002<\/li>\n<\/ul>\n<p>\u6700\u7ec8\uff0cacc \u6570\u7ec4\u5c06\u91c7\u7528 list \u7684\u683c\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u5d4c\u5957\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u7ec4\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>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5d4c\u5957\u7ed3\u6784\u6570\u7ec4 \u7ed9\u5b9a\u4e00\u4e2a\u5d4c\u5957\u7684\u5bf9\u8c61 obj\uff0c\u8981\u6c42\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u7ec4 list\u3002\u6bcf\u4e2a\u6570\u7ec4\u9879\u4ee3\u8868 obj \u4e2d\u7684\u4e00\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u5217\u8868\uff0c\u8f6c\u6362\u4e3a\u4ee5\u4e0b\u683c\u5f0f\uff1a const list = [ { id: 1, \/\/ \u81ea\u52a8\u751f\u6210\u7684 ID name: &#8216;a&#8217;, \/\/ \u5bf9\u8c61\u7684\u952e childList: [ { id: 2, name: &#8216;a1&#8217; }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u9879 { id: 3, name: &#8216;a2&#8217; }, \/\/ \u5b50\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e8c\u9879 ], }, { id: 4, \/\/ \u81ea\u52a8\u751f\u6210\u7684 ID name: &#8216;b&#8217;, \/\/ \u5bf9\u8c61\u7684\u952e childList: [ { id: 5, [&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-1626","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1626","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=1626"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1626\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}