{"id":53511,"date":"2024-12-03T10:02:31","date_gmt":"2024-12-03T02:02:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53511\/"},"modified":"2024-12-03T10:02:31","modified_gmt":"2024-12-03T02:02:31","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-javascript-%e5%af%b9%e8%b1%a1%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%8c%85%e5%90%ab%e5%b5%8c%e5%a5%97-childlist-%e7%9a%84%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53511\/","title":{"rendered":"\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f <\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f \u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733030492674bf25c57d65.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u8f6c\u6362\u4ee5\u4e0b\u539f\u59cb JavaScript \u5bf9\u8c61\u4e3a\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\u7684\u5bf9\u8c61\u6570\u7ec4\uff1f<\/p>\n<p><strong>\u539f\u59cb\u5bf9\u8c61\uff1a<\/strong><\/p>\n<pre>const obj = {\n  \"a\": [\"a1\", \"a2\"],\n  \"b\": [\"b1\", \"b2\", \"b3\"],\n  \/\/ ......\n};<\/pre>\n<p><strong>\u76ee\u6807\u5bf9\u8c61\u6570\u7ec4\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><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Object.entries() \u548c reduce() \u65b9\u6cd5\uff1a<\/p>\n<pre>let obj = {\n  \"a\": [\"a1\", \"a2\"],\n  \"b\": [\"b1\", \"b2\", \"b3\"],\n  \/\/ ......\n};\nlet id = 1;\nObject.entries(obj).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><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<p>\u6b64\u4ee3\u7801\u5c06\u751f\u6210\u4e0e\u76ee\u6807\u5bf9\u8c61\u6570\u7ec4\u76f8\u540c\u7684\u5bf9\u8c61\u6570\u7ec4\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f \u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   win11\u58c1\u7eb8\u88ab\u9501\u5b9a\u66f4\u6362\u4e0d\u4e86\u600e\u4e48\u529e win11\u58c1\u7eb8\u88ab\u9501\u5b9a\u66f4\u6362\u4e0d\u4e86\u8be6\u89e3\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300a\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \u7684\u5bf9\u8c61\u6570\u7ec4\uff1f \u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4 \u95ee\u9898\uff1a \u5982\u4f55\u8f6c\u6362\u4ee5\u4e0b\u539f\u59cb JavaScript \u5bf9\u8c61\u4e3a\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\u7684\u5bf9\u8c61\u6570\u7ec4\uff1f \u539f\u59cb\u5bf9\u8c61\uff1a const obj = { &#8220;a&#8221;: [&#8220;a1&#8221;, &#8220;a2&#8221;], &#8220;b&#8221;: [&#8220;b1&#8221;, &#8220;b2&#8221;, &#8220;b3&#8221;], \/\/ &#8230;&#8230; }; \u76ee\u6807\u5bf9\u8c61\u6570\u7ec4\uff1a const list = [ { id: 1, name: &#8216;a&#8217;, childList: [ { id: 3, name: &#8216;a1&#8217; }, { id: 4, name: [&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-53511","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53511","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=53511"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53511\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}