{"id":1444,"date":"2024-11-10T13:45:45","date_gmt":"2024-11-10T05:45:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1444\/"},"modified":"2024-11-10T13:45:45","modified_gmt":"2024-11-10T05:45:45","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%8c%85%e5%90%ab%e5%b5%8c%e5%a5%97%e6%95%b0%e7%bb%84%e7%9a%84%e5%af%b9%e8%b1%a1%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%8c%85%e5%90%abid%e3%80%81name%e5%92%8c%e5%ad%90%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1444\/","title":{"rendered":"\u5982\u4f55\u5c06\u5305\u542b\u5d4c\u5957\u6570\u7ec4\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542bid\u3001name\u548c\u5b50\u6570\u7ec4\u7684\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173043578336118.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u5305\u542b\u5d4c\u5957\u6570\u7ec4\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542bid\u3001name\u548c\u5b50\u6570\u7ec4\u7684\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u5305\u542b\u5d4c\u5957\u6570\u7ec4\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542bid\u3001name\u548c\u5b50\u6570\u7ec4\u7684\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p>\u539f\u59cb\u5bf9\u8c61\u5305\u542b\u5d4c\u5957\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e2a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5177\u6709 id\u3001name \u548c childList \u5b50\u6570\u7ec4\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 Object.entries() \u548c reduce() \u51fd\u6570\u3002<\/p>\n<pre>let obj = {\n  \"a\": [\n    \"a1\", \"a2\"\n  ],\n  \"b\": [\n    \"b1\", \"b2\", \"b3\" \/\/ \u6570\u7ec4\u5185\u4e2a\u6570\u4e0d\u5b9a\n  ],\n  \/\/ ......\n};\nlet 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>\u6b64\u4ee3\u7801\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u6570\u7ec4\uff1a<\/p>\n<pre>[\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u5305\u542b\u5d4c\u5957\u6570\u7ec4\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542bid\u3001name\u548c\u5b50\u6570\u7ec4\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\u5bf9\u8c61\u6570\u7ec4 \u539f\u59cb\u5bf9\u8c61\u5305\u542b\u5d4c\u5957\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e2a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5177\u6709 id\u3001name \u548c childList \u5b50\u6570\u7ec4\u7684\u5bf9\u8c61\u3002 \u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 Object.entries() \u548c reduce() \u51fd\u6570\u3002 let 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; }; let id = 1; Object.entries(obj).reduce((acc, [name, val]) =&gt; { acc.push({id: id++, name, childList: val.map(name =&gt; ({id: id++, name}))}); return acc; }, []); \u767b\u5f55\u540e\u590d\u5236 \u6b64\u4ee3\u7801\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u6570\u7ec4\uff1a [&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-1444","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1444","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=1444"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1444\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}