{"id":317,"date":"2024-12-17T09:08:56","date_gmt":"2024-12-17T01:08:56","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=317"},"modified":"2024-12-17T09:08:56","modified_gmt":"2024-12-17T01:08:56","slug":"%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e7%a7%bb%e9%99%a4-javascript-%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc%e5%85%83%e7%b4%a0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/17\/%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e7%a7%bb%e9%99%a4-javascript-%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc%e5%85%83%e7%b4%a0%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u9ad8\u6548\u5730\u79fb\u9664 JavaScript \u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\u5143\u7d20\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 JavaScript \u4e2d\uff0c\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u7a7a\u5143\u7d20\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u7a7a\u5143\u7d20\u53ef\u4ee5\u662f <code>null<\/code>\u3001<code>undefined<\/code>\u3001<code>\"\"<\/code>\uff08\u7a7a\u5b57\u7b26\u4e32\uff09\u3001<code>false<\/code>\u3001<code>0<\/code>\u3001<code>NaN<\/code> \u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u8fd9\u4e9b\u7a7a\u5143\u7d20\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 1: \u4f7f\u7528 <code>Array.prototype.filter()<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>filter()<\/code> \u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u6e05\u7406\u6570\u7ec4\u7a7a\u5143\u7d20\u7684\u65b9\u5f0f\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5305\u542b\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\nconst cleanedArr = arr.filter(element =&gt; element);\nconsole.log(cleanedArr);  \/\/ &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>filter()<\/code> \u4f1a\u8fd4\u56de\u6240\u6709 <strong>\u771f\u503c<\/strong>\uff08truthy\uff09\u5143\u7d20\uff0c\u81ea\u52a8\u79fb\u9664 <code>null<\/code>\u3001<code>undefined<\/code>\u3001<code>\"\"<\/code>\uff08\u7a7a\u5b57\u7b26\u4e32\uff09\u3001<code>false<\/code>\u3001<code>0<\/code> \u548c <code>NaN<\/code> \u7b49\u5047\u503c\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u5e38\u89c1\u7684\u201c\u7a7a\u503c\u201d\u5220\u9664\u975e\u5e38\u7b80\u6d01\u4e14\u9ad8\u6548\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 2: \u4f7f\u7528 <code>Array.prototype.filter()<\/code> \u914d\u5408\u81ea\u5b9a\u4e49\u5224\u65ad<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u53ea\u60f3\u5220\u9664\u67d0\u4e9b\u7c7b\u578b\u7684\u7a7a\u5143\u7d20\uff08\u5982 <code>null<\/code> \u548c <code>undefined<\/code>\uff09\uff0c\u800c\u4fdd\u7559\u5176\u4ed6\u7684\u201c\u7a7a\u201d\u5143\u7d20\uff0c\u53ef\u4ee5\u63d0\u4f9b\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u6761\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\nconst cleanedArr = arr.filter(element =&gt; element !== null &amp;&amp; element !== undefined);\nconsole.log(cleanedArr);  \/\/ &#91;1, 2, '', 3, false, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u4f1a\u5220\u9664 <code>null<\/code> \u548c <code>undefined<\/code>\uff0c\u5176\u4ed6\u5047\u503c\uff08\u5982\u7a7a\u5b57\u7b26\u4e32\u3001<code>false<\/code> \u7b49\uff09\u5c06\u88ab\u4fdd\u7559\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 3: \u4f7f\u7528 <code>Array.prototype.forEach()<\/code> \u548c <code>splice()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u5c31\u5730\u4fee\u6539\u6570\u7ec4\u5e76\u5220\u9664\u7a7a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>forEach()<\/code> \u914d\u5408 <code>splice()<\/code> \u6765\u4fee\u6539\u539f\u6570\u7ec4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\narr.forEach((element, index) =&gt; {\n  if (!element) {\n    arr.splice(index, 1);\n  }\n});\nconsole.log(arr);  \/\/ &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u5e76\u5220\u9664\u5047\u503c\u3002<\/li>\n\n\n\n<li><code>splice()<\/code> \u4f1a\u5728\u539f\u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u7ec4\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u6240\u4ee5\u9700\u8981\u5c0f\u5fc3\u4f7f\u7528\uff0c\u5c24\u5176\u662f\u5728\u5220\u9664\u5143\u7d20\u65f6\uff0c\u9700\u8981\u4ece\u540e\u5f80\u524d\u904d\u5386\u6570\u7ec4\uff0c\u907f\u514d\u7d22\u5f15\u9519\u4f4d\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 4: \u4f7f\u7528 <code>Array.prototype.reduce()<\/code> \u8fdb\u884c\u53bb\u7a7a\u64cd\u4f5c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>reduce()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u7d2f\u52a0\u5668\u7684\u65b9\u5f0f\u8fc7\u6ee4\u7a7a\u5143\u7d20\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\nconst cleanedArr = arr.reduce((acc, element) =&gt; {\n  if (element) {\n    acc.push(element);\n  }\n  return acc;\n}, &#91;]);\nconsole.log(cleanedArr);  \/\/ &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>reduce()<\/code> \u65b9\u6cd5\u901a\u8fc7\u7d2f\u52a0\u5668\u9010\u4e2a\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u63a8\u5165\u65b0\u6570\u7ec4\u4e2d\u3002<\/li>\n\n\n\n<li>\u5b83\u5141\u8bb8\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u8fdb\u884c\u81ea\u5b9a\u4e49\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 5: \u4f7f\u7528 <code>Array.prototype.map()<\/code> \u914d\u5408 <code>filter()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5904\u7406\uff0c\u6bd4\u5982\u5220\u9664\u7a7a\u503c\u540e\u5bf9\u5269\u4f59\u7684\u5143\u7d20\u8fdb\u884c\u6620\u5c04\u5904\u7406\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 <code>map()<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>filter()<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\nconst cleanedArr = arr.map(element =&gt; element).filter(element =&gt; element != null);\nconsole.log(cleanedArr);  \/\/ &#91;1, 2, '', 3, false, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>map()<\/code> \u5728\u8fd9\u91cc\u7528\u4f5c\u201c\u4fdd\u7559\u5143\u7d20\u201d\u7684\u64cd\u4f5c\uff0c<code>filter()<\/code> \u5219\u5220\u9664 <code>null<\/code> \u6216 <code>undefined<\/code>\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 6: \u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u624b\u52a8\u5220\u9664\u7a7a\u5143\u7d20<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u5728\u4e0d\u4f7f\u7528\u5185\u5efa\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u81ea\u5b9a\u4e49\u6e05\u7406\u903b\u8f91\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f20\u7edf\u7684 <code>for<\/code> \u5faa\u73af\u6765\u904d\u5386\u6570\u7ec4\u5e76\u5220\u9664\u7a7a\u5143\u7d20\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\nconst cleanedArr = &#91;];\nfor (let i = 0; i &lt; arr.length; i++) {\n  if (arr&#91;i]) {\n    cleanedArr.push(arr&#91;i]);\n  }\n}\nconsole.log(cleanedArr);  \/\/ &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u7528\u4f20\u7edf\u7684\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u624b\u52a8\u5c06\u975e\u7a7a\u503c\u63a8\u5165\u65b0\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u624b\u52a8\u904d\u5386\u53ef\u4ee5\u8ba9\u4f60\u7cbe\u786e\u63a7\u5236\u6bcf\u4e2a\u5143\u7d20\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 7: \u4f7f\u7528 <code>Set<\/code> \u6765\u53bb\u91cd\u5e76\u5220\u9664\u7a7a\u5143\u7d20<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u5220\u9664\u7a7a\u5143\u7d20\u5e76\u53bb\u91cd\u6570\u7ec4\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528 <code>filter()<\/code> \u548c <code>Set<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4, 1, 2];\nconst cleanedArr = &#91;...new Set(arr.filter(Boolean))];\nconsole.log(cleanedArr);  \/\/ &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>filter(Boolean)<\/code> \u5220\u9664\u6240\u6709\u5047\u503c\uff0c<code>new Set()<\/code> \u53ef\u4ee5\u53bb\u91cd\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u53bb\u9664\u7a7a\u5143\u7d20\u4e14\u786e\u4fdd\u6570\u7ec4\u4e2d\u6ca1\u6709\u91cd\u590d\u5143\u7d20\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 8: \u4f7f\u7528 <code>Array.prototype.find()<\/code> \u6216 <code>Array.prototype.findIndex()<\/code> \u5220\u9664\u7a7a\u5143\u7d20<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u7528 <code>find()<\/code> \u6216 <code>findIndex()<\/code> \u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u7a7a\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528 <code>splice()<\/code> \u5220\u9664\u5b83\u4eec\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, null, 2, undefined, '', 3, false, 4];\nlet index;\nwhile ((index = arr.findIndex(element =&gt; !element)) !== -1) {\n  arr.splice(index, 1);\n}\nconsole.log(arr);  \/\/ &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u7528 <code>findIndex()<\/code> \u67e5\u627e\u7b2c\u4e00\u4e2a\u7a7a\u5143\u7d20\uff0c\u5e76\u7528 <code>splice()<\/code> \u5220\u9664\u5b83\uff0c\u76f4\u5230\u6ca1\u6709\u7a7a\u5143\u7d20\u4e3a\u6b62\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>filter()<\/code><\/strong> \u662f\u6700\u7b80\u6d01\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u90e8\u5206\u9700\u6c42\uff0c\u5c24\u5176\u662f\u53bb\u9664\u6240\u6709\u5047\u503c\u3002<\/li>\n\n\n\n<li><strong><code>forEach()<\/code> + <code>splice()<\/code><\/strong> \u548c <strong><code>reduce()<\/code><\/strong> \u65b9\u6cd5\u9002\u5408\u90a3\u4e9b\u9700\u8981\u5bf9\u6570\u7ec4\u8fdb\u884c\u539f\u5730\u4fee\u6539\u6216\u66f4\u52a0\u590d\u6742\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u5220\u9664\u7a7a\u5143\u7d20\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>filter()<\/code> \u6216 <code>reduce()<\/code>\uff0c\u5e76\u7ed3\u5408\u81ea\u5df1\u7684\u903b\u8f91\u8fdb\u884c\u8c03\u6574\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u6839\u636e\u4f60\u7684\u9700\u6c42\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u7a7a\u5143\u7d20\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u7a7a\u5143\u7d20\u53ef\u4ee5\u662f null\u3001undefined\u3001&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/317\/revisions\/318"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}