{"id":52901,"date":"2024-12-03T15:24:52","date_gmt":"2024-12-03T07:24:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52901\/"},"modified":"2024-12-03T15:24:52","modified_gmt":"2024-12-03T07:24:52","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e9%80%92%e5%bd%92%e5%b0%86%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%97%e8%a1%a8%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52901\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f<\/h1>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324354186742ddda5b419.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 javascript \u9012\u5f52\u5c06\u6811\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e<\/strong><\/p>\n<p>\u5728\u9762\u8bd5\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u8981\u6c42\u4f7f\u7528 javascript \u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u6210\u5217\u8868\u6570\u636e\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u3002<\/p>\n<pre>\/**\ninput: [{\n    id: 1,\n    text: 'text1',\n    children: [{\n        id: 2,\n        text: 'text2',\n        parentId: 1,\n        children: [{\n            id: 4,\n            text: 'text4',\n            parentId: 2\n        }]\n    }, {\n        id: 3,\n        text: 'text3',\n        parentId: 1\n    }]\n}]\noutput: [{\n    id: 4,\n    text: 'text4',\n    parentId: 2\n}, {\n    id: 2,\n    text: 'text2',\n    parentId: 1\n}, {\n    id: 3,\n    text: 'text3',\n    parentId: 1\n}, {\n    id: 1,\n    text: 'text1'\n}, ];\n**\/\nfunction walk(list) {\n  var output = [];\n\n  list.forEach(function(item) {\n    if (item.children) {\n      output = output.concat(walk(item.children));\n      delete item.children;\n    }\n    output.push(item);\n  });\n\n  return output;\n}<\/pre>\n<p>\u5728\u4ee3\u7801\u4e2d\uff1a<\/p>\n<ul>\n<li>output \u6570\u7ec4\u7528\u4e8e\u5b58\u50a8\u8f6c\u6362\u540e\u7684\u5217\u8868\u6570\u636e\u3002<\/li>\n<li>\u904d\u5386\u7ed9\u5b9a\u7684\u6811\u5f62\u7ed3\u6784\u5217\u8868\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u6709 children \u5c5e\u6027\u3002<\/li>\n<li>\u5982\u679c\u5b58\u5728 children \u5c5e\u6027\uff0c\u9012\u5f52\u8c03\u7528 walk \u51fd\u6570\u4ee5\u8f6c\u6362\u5b50\u6811\u3002<\/li>\n<li>\u5c06\u8f6c\u6362\u540e\u7684\u5b50\u6811\u6570\u636e\u6dfb\u52a0\u5230 output \u6570\u7ec4\u4e2d\u5e76\u5220\u9664 children \u5c5e\u6027\u4ee5\u907f\u514d\u5faa\u73af\u5f15\u7528\u3002<\/li>\n<li>\u5c06\u5f53\u524d\u5143\u7d20\u6dfb\u52a0\u5230 output \u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u6700\u7ec8\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u5217\u8868\u6570\u636e\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u9012\u5f52\u5730\u904d\u5386\u7ed9\u5b9a\u7684\u6811\u5f62\u7ed3\u6784\uff0c\u5e76\u5c06\u6240\u6709\u8282\u70b9\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u6570\u636e\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   win11\u58f0\u97f3\u5ffd\u5927\u5ffd\u5c0f\u600e\u4e48\u89e3\u51b3 win11\u58f0\u97f3\u5ffd\u5927\u5ffd\u5c0f\u600e\u4e48\u89e3\u51b3\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u9012\u5f52\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u5982\u4f55\u4f7f\u7528 javascript \u9012\u5f52\u5c06\u6811\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e \u5728\u9762\u8bd5\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u8981\u6c42\u4f7f\u7528 javascript \u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u6210\u5217\u8868\u6570\u636e\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u3002 \/** input: [{ id: 1, text: &#8216;text1&#8217;, children: [{ id: 2, text: &#8216;text2&#8217;, parentId: 1, children: [{ id: 4, text: &#8216;text4&#8217;, parentId: 2 }] }, { id: 3, text: &#8216;text3&#8217;, parentId: 1 }] }] output: [{ id: 4, text: &#8216;text4&#8217;, parentId: 2 }, [&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-52901","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52901","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=52901"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52901\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}