{"id":1857,"date":"2024-11-10T13:09:51","date_gmt":"2024-11-10T05:09:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1857\/"},"modified":"2024-11-10T13:09:51","modified_gmt":"2024-11-10T05:09:51","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","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1857\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173060487358719.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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u76ee\u6807\u5bf9\u8c61\u6570\u7ec4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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<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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5305\u542b\u5d4c\u5957 childList \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>\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; }; \u767b\u5f55\u540e\u590d\u5236 \u76ee\u6807\u5bf9\u8c61\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 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;, [&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-1857","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1857","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=1857"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}