{"id":52508,"date":"2024-12-03T10:30:48","date_gmt":"2024-12-03T02:30:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52508\/"},"modified":"2024-12-03T10:30:48","modified_gmt":"2024-12-03T02:30:48","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e5%b0%86%e6%95%b0%e7%bb%84%e4%b8%ad%e4%b8%8e%e7%89%b9%e5%ae%9a%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8c%b9%e9%85%8d%e7%9a%84%e5%85%83%e7%b4%a0%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52508\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\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\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\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\/20241121\/1732155008673e9680447d4.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7528 js \u5c06\u6570\u7ec4\u4e2d\u7279\u5b9a\u540d\u79f0\u503c\u7f6e\u7a7a<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 list\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5177\u6709 name \u5c5e\u6027\u3002\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u7684\u65b9\u6cd5\uff0c\u5728 aa \u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u4e0e list \u4e2d\u5404 name \u503c\u5339\u914d\u7684\u9879\u65f6\uff0c\u5c06\u540d\u79f0\u7f6e\u7a7a\uff1f<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u7ed9\u5b9a\uff1a<\/p>\n<pre>var list = [\n  {\n      \"name\": \"\u82b1\",\n      \"index\": 10,\n      \"len\": 4,\n      \"left\": 150,\n      \"bottom\": 108\n  },\n  {\n      \"name\": \"\u82b1\",\n      \"index\": 0,\n      \"len\": 4,\n      \"left\": 150,\n      \"bottom\": 0\n  },\n  {\n      \"name\": \"\u82b1\",\n      \"index\": 8,\n      \"len\": 4,\n      \"left\": 950,\n      \"bottom\": 0\n  },\n  {\n      \"name\": \"\u8349\",\n      \"index\": 14,\n      \"len\": 4,\n      \"left\": 550,\n      \"bottom\": 108\n  },\n  {\n      \"name\": \"\u8349\",\n      \"index\": 15,\n      \"len\": 4,\n      \"left\": 650,\n      \"bottom\": 108\n  },\n  {\n      \"name\": \"\u8349\",\n      \"index\": 15,\n      \"len\": 4,\n      \"left\": 650,\n      \"bottom\": 108\n  }\n];\naa = '\u82b1\u82b1\u8349\u8349';<\/pre>\n<\/li>\n<li>\n<p>\u9884\u671f\u7ed3\u679c\uff1a<\/p>\n<pre>list = [\n  {\n      \"name\": \"\u82b1\",\n      \"index\": 10,\n      \"len\": 4,\n      \"left\": 150,\n      \"bottom\": 108\n  },\n  {\n      \"name\": \"\",\n      \"index\": 0,\n      \"len\": 4,\n      \"left\": 150,\n      \"bottom\": 0\n  },\n  {\n      \"name\": \"\",\n      \"index\": 8,\n      \"len\": 4,\n      \"left\": 950,\n      \"bottom\": 0\n  },\n  {\n      \"name\": \"\",\n      \"index\": 14,\n      \"len\": 4,\n      \"left\": 550,\n      \"bottom\": 108\n  },\n  {\n      \"name\": \"\",\n      \"index\": 15,\n      \"len\": 4,\n      \"left\": 650,\n      \"bottom\": 108\n  },\n  {\n      \"name\": \"\u8349\",\n      \"index\": 15,\n      \"len\": 4,\n      \"left\": 650,\n      \"bottom\": 108\n  }\n];<\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<pre>let start = list.map(i =&gt; i.name).join('').indexOf(aa);\nif (start &gt; -1) {\n    let end = Math.min(start + aa.length, list.length);\n    for (let index = start; index &lt; end; index++) {\n        list[index].name = '';\n    }\n}<\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5c06 list \u4e2d\u7684 name \u5c5e\u6027\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 map \u548c join\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528 indexof \u5728\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e aa \u5b57\u7b26\u4e32\uff0c\u83b7\u53d6\u5176\u8d77\u59cb\u4f4d\u7f6e start\u3002\u5982\u679c start \u4e3a -1\uff0c\u5219\u8bf4\u660e aa \u6ca1\u6709\u5728 list \u4e2d\u627e\u5230\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u8ba1\u7b97 end\uff0c\u5b83\u662f start \u52a0\u4e0a aa \u957f\u5ea6\uff0c\u6216 list \u7684\u957f\u5ea6\uff08\u53d6\u8f83\u5c0f\u503c\uff09\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u904d\u5386\u8303\u56f4 [start, end) \u5185\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5b83\u4eec\u7684 name \u5c5e\u6027\u7f6e\u7a7a\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u63ed\u79d8\u7535\u8111\u6444\u50cf\u5934\u62cd\u7167\u7684\u6280\u5de7\u4e0e\u7a8d\u95e8\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\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\u4f7f\u7528 JavaScript \u5c06\u6570\u7ec4\u4e2d\u4e0e\u7279\u5b9a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u5143\u7d20\u7684\u540d\u79f0\u7f6e\u7a7a\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 \u7528 js \u5c06\u6570\u7ec4\u4e2d\u7279\u5b9a\u540d\u79f0\u503c\u7f6e\u7a7a \u95ee\u9898\uff1a \u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 list\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5177\u6709 name \u5c5e\u6027\u3002\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u7684\u65b9\u6cd5\uff0c\u5728 aa \u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u4e0e list \u4e2d\u5404 name \u503c\u5339\u914d\u7684\u9879\u65f6\uff0c\u5c06\u540d\u79f0\u7f6e\u7a7a\uff1f \u793a\u4f8b\uff1a \u7ed9\u5b9a\uff1a var list = [ { &#8220;name&#8221;: &#8220;\u82b1&#8221;, &#8220;index&#8221;: 10, &#8220;len&#8221;: 4, &#8220;left&#8221;: 150, &#8220;bottom&#8221;: 108 }, { &#8220;name&#8221;: &#8220;\u82b1&#8221;, &#8220;index&#8221;: 0, &#8220;len&#8221;: 4, &#8220;left&#8221;: 150, &#8220;bottom&#8221;: 0 }, { &#8220;name&#8221;: &#8220;\u82b1&#8221;, &#8220;index&#8221;: 8, [&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-52508","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52508","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=52508"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52508\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}