{"id":25836,"date":"2024-11-24T14:04:48","date_gmt":"2024-11-24T06:04:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25836\/"},"modified":"2024-11-24T14:04:48","modified_gmt":"2024-11-24T06:04:48","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e7%9c%81%e5%b8%82%e5%8c%ba%e6%a0%91%e7%bb%93%e6%9e%84%e6%89%81%e5%b9%b3%e5%8c%96%e4%b8%ba%e9%80%89%e4%b8%ad%e8%8a%82%e7%82%b9%e7%9a%84%e4%bb%a3%e7%a0%81%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25836\/","title":{"rendered":"\u5982\u4f55\u5c06\u7701\u5e02\u533a\u6811\u7ed3\u6784\u6241\u5e73\u5316\u4e3a\u9009\u4e2d\u8282\u70b9\u7684\u4ee3\u7801\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173071351456664.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u7701\u5e02\u533a\u6811\u7ed3\u6784\u6241\u5e73\u5316\u4e3a\u9009\u4e2d\u8282\u70b9\u7684\u4ee3\u7801\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u7701\u5e02\u533a\u6811\u7ed3\u6784\u6241\u5e73\u5316\u4e3a\u9009\u4e2d\u8282\u70b9\u7684\u4ee3\u7801\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7701\u5e02\u533a\u6811\u7ed3\u6784\u6241\u5e73\u5316\u8f6c\u6362<\/strong><\/p>\n<p>\u7ed9\u4e86\u7701\u5e02\u533a\u7684\u6811\u72b6\u7ed3\u6784\uff0c\u53ea\u6709\u9009\u4e2d\u8282\u70b9\u7684check\u4e3a1\uff0c\u5176\u4ed6\u90fd\u4e3anull\u3002\u6211\u4eec\u9700\u8981\u505a\u7684\u5c31\u662f\u6241\u5e73\u5316\u6570\u636e\u7ed3\u6784\uff0c\u628a\u6240\u6709\u88ab\u9009\u4e2d\u7684\u7701\u3001\u5e02\u3001\u533a\u4ee3\u7801\u653e\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u601d\u8def\u662f\u9012\u5f52\u904d\u5386\u6811\u7ed3\u6784\uff0c\u5c06\u9009\u4e2d\u72b6\u6001\u5411\u4e0b\u4f20\u9012\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570getCheckedList\uff0c\u63a5\u53d7\u6811\u5f62\u7ed3\u6784list\u3001\u5f53\u524d\u8def\u5f84parentList\u548c\u4e0a\u7ea7\u662f\u5426\u88ab\u9009\u4e2dparentChecked\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li>\u521d\u59cb\u5316\u7ed3\u679c\u96c6result\u4e3a\u7a7a\u6570\u7ec4\u3002<\/li>\n<li>\n<p>\u904d\u5386list\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u8282\u70b9\uff1a<\/p>\n<ul>\n<li>\u8ba1\u7b97\u5f53\u524d\u8282\u70b9\u7684\u9009\u4e2d\u72b6\u6001checked\uff0c\u5982\u679c\u4e0a\u7ea7\u88ab\u9009\u4e2d\u6216\u5f53\u524d\u88ab\u9009\u4e2d\uff0c\u5219\u8ba4\u4e3a\u88ab\u9009\u4e2d\u3002<\/li>\n<li>\u5c06\u5f53\u524d\u8282\u70b9\u7684code\u6dfb\u52a0\u5230\u5f53\u524d\u8def\u5f84\u4e2d\uff0c\u5f97\u5230\u65b0\u7684codeList\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u8282\u70b9\u8fd8\u6709\u5b50\u8282\u70b9\uff0c\u5219\u9012\u5f52\u8c03\u7528getCheckedList\uff0c\u5c06codeList\u548cchecked\u5411\u4e0b\u4f20\u9012\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u53f6\u5b50\u8282\u70b9\uff0c\u4e14\u88ab\u9009\u4e2d\uff0c\u5219\u5c06codeList\u6dfb\u52a0\u5230\u7ed3\u679c\u96c6\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u7ed3\u679c\u96c6result\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<pre>const getCheckedList = (list, parentList = [], parentChecked = false) =&gt; {\n  let result = [];\n\n  if (!Array.isArray(list)) {\n    return result;\n  }\n\n  list.forEach((item) =&gt; {\n    const checked = parentChecked || item.check; \/\/ \u7236\u7ea7\u88ab\u9009\u4e2d\u6216\u5f53\u524d\u88ab\u9009\u4e2d\uff0c\u5747\u8ba4\u4e3a\u662f\u88ab\u9009\u4e2d\n    const codeList = parentList.concat(item.code);\n\n    if (item.children) {\n      \/\/ \u5f53\u524d\u4e0d\u662f\u6700\u5185\u5c42\n      result = result.concat(getCheckedList(item.children, codeList, checked));\n    } else {\n      \/\/ \u5df2\u5230\u6700\u5185\u5c42\n      if (checked) {\n        result.push(codeList);\n      }\n    }\n  });\n\n  return result;\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre>const tree = [\n  {\n    \"code\": \"110000\",\n    \"value\": \"\u5317\u4eac\u5e02\",\n    \"check\": 1,\n    \"children\": [\n      {\n        \"code\": \"110100\",\n        \"value\": \"\u5317\u4eac\u5e02\",\n        \"check\": null,\n        \"children\": [\n          {\n            \"code\": \"110101\",\n            \"value\": \"\u4e1c\u57ce\u533a\",\n            \"check\": null\n          },\n          {\n            \"code\": \"110102\",\n            \"value\": \"\u897f\u57ce\u533a\",\n            \"checked\": null\n          }\n        ]\n      }\n    ]\n  },\n  \/* \u7701\u7565\u5176\u4ed6\u7701\u5e02\u533a *\/\n];\n\nconst result = getCheckedList(tree);\nconsole.log(result);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre>[\n  [ \"110000\", \"110100\", \"110101\" ],\n  [ \"110000\", \"110100\", \"110102\" ],\n  \/* \u7701\u7565\u5176\u4ed6\u9009\u4e2d\u9879 *\/\n]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u7701\u5e02\u533a\u6811\u7ed3\u6784\u6241\u5e73\u5316\u4e3a\u9009\u4e2d\u8282\u70b9\u7684\u4ee3\u7801\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>\u7701\u5e02\u533a\u6811\u7ed3\u6784\u6241\u5e73\u5316\u8f6c\u6362 \u7ed9\u4e86\u7701\u5e02\u533a\u7684\u6811\u72b6\u7ed3\u6784\uff0c\u53ea\u6709\u9009\u4e2d\u8282\u70b9\u7684check\u4e3a1\uff0c\u5176\u4ed6\u90fd\u4e3anull\u3002\u6211\u4eec\u9700\u8981\u505a\u7684\u5c31\u662f\u6241\u5e73\u5316\u6570\u636e\u7ed3\u6784\uff0c\u628a\u6240\u6709\u88ab\u9009\u4e2d\u7684\u7701\u3001\u5e02\u3001\u533a\u4ee3\u7801\u653e\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002 \u89e3\u51b3\u65b9\u6848 \u601d\u8def\u662f\u9012\u5f52\u904d\u5386\u6811\u7ed3\u6784\uff0c\u5c06\u9009\u4e2d\u72b6\u6001\u5411\u4e0b\u4f20\u9012\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570getCheckedList\uff0c\u63a5\u53d7\u6811\u5f62\u7ed3\u6784list\u3001\u5f53\u524d\u8def\u5f84parentList\u548c\u4e0a\u7ea7\u662f\u5426\u88ab\u9009\u4e2dparentChecked\u4f5c\u4e3a\u53c2\u6570\u3002 \u521d\u59cb\u5316\u7ed3\u679c\u96c6result\u4e3a\u7a7a\u6570\u7ec4\u3002 \u904d\u5386list\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u8282\u70b9\uff1a \u8ba1\u7b97\u5f53\u524d\u8282\u70b9\u7684\u9009\u4e2d\u72b6\u6001checked\uff0c\u5982\u679c\u4e0a\u7ea7\u88ab\u9009\u4e2d\u6216\u5f53\u524d\u88ab\u9009\u4e2d\uff0c\u5219\u8ba4\u4e3a\u88ab\u9009\u4e2d\u3002 \u5c06\u5f53\u524d\u8282\u70b9\u7684code\u6dfb\u52a0\u5230\u5f53\u524d\u8def\u5f84\u4e2d\uff0c\u5f97\u5230\u65b0\u7684codeList\u3002 \u5982\u679c\u5f53\u524d\u8282\u70b9\u8fd8\u6709\u5b50\u8282\u70b9\uff0c\u5219\u9012\u5f52\u8c03\u7528getCheckedList\uff0c\u5c06codeList\u548cchecked\u5411\u4e0b\u4f20\u9012\u3002 \u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u53f6\u5b50\u8282\u70b9\uff0c\u4e14\u88ab\u9009\u4e2d\uff0c\u5219\u5c06codeList\u6dfb\u52a0\u5230\u7ed3\u679c\u96c6\u4e2d\u3002 \u8fd4\u56de\u7ed3\u679c\u96c6result\u3002 \u4ee3\u7801\u793a\u4f8b const getCheckedList = (list, parentList = [], parentChecked = false) =&gt; { let result = []; if (!Array.isArray(list)) { return result; } list.forEach((item) =&gt; { const checked = parentChecked || item.check; \/\/ \u7236\u7ea7\u88ab\u9009\u4e2d\u6216\u5f53\u524d\u88ab\u9009\u4e2d\uff0c\u5747\u8ba4\u4e3a\u662f\u88ab\u9009\u4e2d const codeList = parentList.concat(item.code); if (item.children) { \/\/ \u5f53\u524d\u4e0d\u662f\u6700\u5185\u5c42 [&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-25836","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25836","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=25836"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25836\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}