{"id":1399,"date":"2024-11-10T12:57:49","date_gmt":"2024-11-10T04:57:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1399\/"},"modified":"2024-11-10T12:57:49","modified_gmt":"2024-11-10T04:57:49","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%a4%8d%e6%9d%82%e5%af%b9%e8%b1%a1%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%bb%93%e6%9e%84%e5%8c%96%e7%9a%84%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1399\/","title":{"rendered":"\u5982\u4f55\u5c06\u590d\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684\u5bf9\u8c61\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173042265651386.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u590d\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684\u5bf9\u8c61\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u590d\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684\u5bf9\u8c61\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u7e41\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u590d\u6742\u7684\u5bf9\u8c61\u5bf9\u8c61\uff0c\u5176\u952e\u548c\u503c\u5206\u522b\u4ee3\u8868\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u6570\u7ec4\u503c\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c06\u8be5\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u5305\u542b\u4e00\u4e2a id\u3001\u4e00\u4e2a\u540d\u79f0\u548c\u4e00\u4e2a\u5305\u542b\u5b50\u5bf9\u8c61\u540d\u79f0\u7684 childList\u3002<\/p>\n<p><strong>\u539f\u59cb\u6570\u636e\uff1a<\/strong><\/p>\n<pre>const obj = {\n  \"a\": [\n    \"a1\", \"a2\"\n  ],\n  \"b\": [\n    \"b1\", \"b2\", \"b3\" \/\/ \u6570\u7ec4\u5185\u4e2a\u6570\u4e0d\u5b9a\n  ],\n  \/\/......\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u76ee\u6807\u6570\u636e\uff1a<\/strong><\/p>\n<pre>const list = [\n  {\n    id: 1,\n    name: 'a', \n    childList:[\n      {id: 3, name: 'a1'},\n      {id: 4, name: 'a2'},\n    ]\n  },\n  {\n    id: 2,\n    name: 'b',\n    childList:[\n      {id: 5, name: 'b1'},\n      {id: 6, name: 'b2'},\n      {id: 7, name: 'b2'},\n    ]\n  }\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<pre>let id=1;\nObject.entries(obj).reduce((acc,[name, val])=&gt;{\n    acc.push({id:id++, name, childList:val.map(name=&gt;({id:id++, name}))});\n    return acc;\n},[]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5b9e\u73b0\u7ec6\u8282\uff1a<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\u521b\u5efa\u4e00\u4e2a id \u8ba1\u6570\u5668\uff0c\u4ece 1 \u5f00\u59cb\u3002<\/li>\n<li>\u4f7f\u7528 Object.entries() \u904d\u5386\u539f\u59cb\u5bf9\u8c61\u7684\u3002<\/li>\n<li>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u952e\u503c\u5bf9\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff1a<\/p>\n<ul>\n<li>id\uff1a\u4f7f\u7528 id \u8ba1\u6570\u5668\u5e76\u9012\u589e\u3002<\/li>\n<li>name\uff1a\u952e\u3002<\/li>\n<li>childList\uff1a\u5c06\u503c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b id \u548c name \u5c5e\u6027\u7684\u65b0\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6211\u4eec\u4f7f\u7528 reduce() \u7d2f\u79ef\u6240\u6709\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u5230 acc \u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u6700\u540e\uff0c\u8fd4\u56de acc \u6570\u7ec4\u4f5c\u4e3a\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u590d\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684\u5bf9\u8c61\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>\u5982\u4f55\u5c06\u7e41\u6742\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684\u5bf9\u8c61\u6570\u7ec4 \u95ee\u9898\uff1a \u7ed9\u5b9a\u4e00\u4e2a\u590d\u6742\u7684\u5bf9\u8c61\u5bf9\u8c61\uff0c\u5176\u952e\u548c\u503c\u5206\u522b\u4ee3\u8868\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u6570\u7ec4\u503c\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c06\u8be5\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u5305\u542b\u4e00\u4e2a id\u3001\u4e00\u4e2a\u540d\u79f0\u548c\u4e00\u4e2a\u5305\u542b\u5b50\u5bf9\u8c61\u540d\u79f0\u7684 childList\u3002 \u539f\u59cb\u6570\u636e\uff1a const obj = { &#8220;a&#8221;: [ &#8220;a1&#8221;, &#8220;a2&#8221; ], &#8220;b&#8221;: [ &#8220;b1&#8221;, &#8220;b2&#8221;, &#8220;b3&#8221; \/\/ \u6570\u7ec4\u5185\u4e2a\u6570\u4e0d\u5b9a ], \/\/&#8230;&#8230; }; \u767b\u5f55\u540e\u590d\u5236 \u76ee\u6807\u6570\u636e\uff1a const list = [ { id: 1, name: &#8216;a&#8217;, childList:[ {id: 3, name: &#8216;a1&#8217;}, {id: 4, name: &#8216;a2&#8217;}, ] }, { id: 2, name: &#8216;b&#8217;, 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-1399","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1399","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=1399"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1399\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}