{"id":3749,"date":"2024-11-10T13:45:13","date_gmt":"2024-11-10T05:45:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3749\/"},"modified":"2024-11-10T13:45:13","modified_gmt":"2024-11-10T05:45:13","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%80%92%e5%bd%92%e5%ae%9e%e7%8e%b0-javascript-%e4%b8%ad%e7%9a%84-walk-%e5%87%bd%e6%95%b0%ef%bc%8c%e5%b0%86%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3749\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 JavaScript \u4e2d\u7684 walk \u51fd\u6570\uff0c\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173113098136070.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 JavaScript \u4e2d\u7684 walk \u51fd\u6570\uff0c\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 JavaScript \u4e2d\u7684 walk \u51fd\u6570\uff0c\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0 js \u4e2d\u7684 walk \u51fd\u6570<\/strong><\/p>\n<p>\u5bf9\u4e8e\u9762\u8bd5\u4e2d\u7684\u8fd9\u4efd\u9898\u76ee\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 javascript \u4e2d\u7684\u9012\u5f52\u6765\u5b9e\u73b0\u4e00\u4e2a\u540d\u4e3a walk \u7684\u51fd\u6570\u3002\u8be5\u51fd\u6570\u65e8\u5728\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u5b9a\u4e49 walk \u51fd\u6570\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>function walk(list) {\n  \/\/ \u9012\u5f52\u51fd\u6570\u4ee3\u7801\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u9012\u5f52\u904d\u5386\u6811\u5f62\u7ed3\u6784\uff1a<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528\u9012\u5f52\u904d\u5386\u6811\u5f62\u7ed3\u6784\uff0c\u9010\u5c42\u6df1\u5165\u3002<\/p>\n<p><strong>3. \u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\u4fee\u6539\u6570\u636e\uff1a<\/strong><\/p>\n<p>\u5982\u679c encountering \u9047\u5230\u4e86\u5177\u6709 children \u5c5e\u6027\u7684\u8282\u70b9\uff0c\u5219\uff1a<\/p>\n<ul>\n<li>\u5206\u522b\u5bf9 children \u9012\u5f52\u8c03\u7528 walk \u51fd\u6570\uff0c\u5220\u9664 children \u5c5e\u6027\u3002<\/li>\n<li>\u5c06\u8282\u70b9\u81ea\u8eab push \u5230 output \u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>function walk(list) {\n  var output = [];\n  list.foreach(function(item) {\n    if (item.children) {\n      walk(item.children);\n      delete item.children;\n    }\n    output.push(item);\n  })\n  return output;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6837\u4f8b\u8f93\u5165\u548c\u8f93\u51fa\uff1a<\/strong><\/p>\n<p><strong>\u8f93\u5165\uff1a<\/strong><\/p>\n<pre>[{\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}]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[{\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}, ]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 JavaScript \u4e2d\u7684 walk \u51fd\u6570\uff0c\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\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>\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0 js \u4e2d\u7684 walk \u51fd\u6570 \u5bf9\u4e8e\u9762\u8bd5\u4e2d\u7684\u8fd9\u4efd\u9898\u76ee\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 javascript \u4e2d\u7684\u9012\u5f52\u6765\u5b9e\u73b0\u4e00\u4e2a\u540d\u4e3a walk \u7684\u51fd\u6570\u3002\u8be5\u51fd\u6570\u65e8\u5728\u5c06\u6811\u5f62\u7ed3\u6784\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u6570\u636e\u3002 \u5b9e\u73b0\u65b9\u6cd5\uff1a 1. \u5b9a\u4e49 walk \u51fd\u6570\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b function walk(list) { \/\/ \u9012\u5f52\u51fd\u6570\u4ee3\u7801 } \u767b\u5f55\u540e\u590d\u5236 2. \u9012\u5f52\u904d\u5386\u6811\u5f62\u7ed3\u6784\uff1a \u5728\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528\u9012\u5f52\u904d\u5386\u6811\u5f62\u7ed3\u6784\uff0c\u9010\u5c42\u6df1\u5165\u3002 3. \u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\u4fee\u6539\u6570\u636e\uff1a \u5982\u679c encountering \u9047\u5230\u4e86\u5177\u6709 children \u5c5e\u6027\u7684\u8282\u70b9\uff0c\u5219\uff1a \u5206\u522b\u5bf9 children \u9012\u5f52\u8c03\u7528 walk \u51fd\u6570\uff0c\u5220\u9664 children \u5c5e\u6027\u3002 \u5c06\u8282\u70b9\u81ea\u8eab push \u5230 output \u6570\u7ec4\u4e2d\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a function walk(list) { var output = []; list.foreach(function(item) { [&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-3749","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3749","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=3749"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3749\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}