{"id":3508,"date":"2024-11-10T17:43:22","date_gmt":"2024-11-10T09:43:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3508\/"},"modified":"2024-11-10T17:43:22","modified_gmt":"2024-11-10T09:43:22","slug":"%e5%9f%ba%e6%9c%ac%e7%9a%84-javascript-%e6%95%b0%e7%bb%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3508\/","title":{"rendered":"\u57fa\u672c\u7684 JavaScript \u6570\u7ec4\u65b9\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173104762132715.jpg\" class=\"aligncenter\" title=\"\u57fa\u672c\u7684 JavaScript \u6570\u7ec4\u65b9\u6cd5\u63d2\u56fe\" alt=\"\u57fa\u672c\u7684 JavaScript \u6570\u7ec4\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p>javascript \u6570\u7ec4\u5e26\u6709\u5f3a\u5927\u7684\u5355\u884c\u8bed\u53e5\uff0c\u4f7f\u7f16\u7801\u53d8\u5f97\u66f4\u7b80\u5355\u3001\u66f4\u6e05\u6670\u3002\u4ee5\u4e0b\u662f\u638c\u63e1\u4e00\u4e9b\u5173\u952e\u6570\u7ec4\u65b9\u6cd5\u7684\u5feb\u901f\u6307\u5357\uff1a<\/p>\n<p><strong>\u8fc7\u6ee4\u6570\u7ec4\uff1a<\/strong> .filter() \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u901a\u8fc7\u6d4b\u8bd5\u7684\u5143\u7d20\u3002<\/p>\n<pre>const oddnumbers = [1, 2, 3, 4, 5, 6].filter(num =&gt; num % 2 !== 0); \/\/ [1, 3, 5]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6620\u5c04\u6570\u7ec4\uff1a<\/strong> .map() \u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<pre>const doubled = [1, 2, 3, 4, 5].map(num =&gt; num * 2); \/\/ [2, 4, 6, 8, 10]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u51cf\u5c11\u6570\u7ec4\uff1a<\/strong> .reduce() \u5904\u7406\u6240\u6709\u5143\u7d20\u4ee5\u4ea7\u751f\u5355\u4e2a\u7ed3\u679c\u3002<\/p>\n<pre>const sum = [1, 2, 3, 4, 5].reduce((total, num) =&gt; total + num, 0); \/\/ 15\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>*<em>\u67e5\u627e\u5143\u7d20\uff1a*<\/em>.find() \u8fd4\u56de\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<pre>const firsteven = [1, 2, 3, 4, 5].find(num =&gt; num % 2 === 0); \/\/ 2\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u68c0\u67e5\u6761\u4ef6\uff1a<\/strong> .some() \u548c .every() \u68c0\u67e5\u662f\u5426\u6709\u4efb\u4f55\u6216\u6240\u6709\u5143\u7d20\u901a\u8fc7\u6d4b\u8bd5\u3002<\/p>\n<pre>const haseven = [1, 3, 5, 7, 8].some(num =&gt; num % 2 === 0); \/\/ true\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5c55\u5e73\u6570\u7ec4\uff1a<\/strong> .flat() \u5c06\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a\u5355\u5c42\u6570\u7ec4\u3002<\/p>\n<pre>const flattened = [1, [2, 3], [4, [5, 6]]].flat(2); \/\/ [1, 2, 3, 4, 5, 6]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5220\u9664\u91cd\u590d\u9879\uff1a<\/strong>\u4f7f\u7528 set \u5220\u9664\u91cd\u590d\u9879\u3002<\/p>\n<pre>const uniquenumbers = [...new set([1, 2, 2, 3, 4, 4, 5])]; \/\/ [1, 2, 3, 4, 5]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/strong> .sort() \u6570\u5b57\u3002<\/p>\n<pre>const sortedNumbers = [5, 2, 9, 1, 5, 6].sort((a, b) =&gt; a - b); \/\/ [1, 2, 5, 5, 6, 9]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e9b\u5355\u884c\u4ee3\u7801\u53ef\u4ee5\u663e\u7740\u7b80\u5316\u60a8\u7684\u4ee3\u7801\u3002 <strong>\u8981\u6df1\u5165\u4e86\u89e3\uff0c\u8bf7\u67e5\u770b\u6211\u7684 javascript \u5907\u5fd8\u5355\u4ee5\u53ca\u66f4\u591a\u5185\u5bb9<\/strong><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u57fa\u672c\u7684 JavaScript \u6570\u7ec4\u65b9\u6cd5\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>javascript \u6570\u7ec4\u5e26\u6709\u5f3a\u5927\u7684\u5355\u884c\u8bed\u53e5\uff0c\u4f7f\u7f16\u7801\u53d8\u5f97\u66f4\u7b80\u5355\u3001\u66f4\u6e05\u6670\u3002\u4ee5\u4e0b\u662f\u638c\u63e1\u4e00\u4e9b\u5173\u952e\u6570\u7ec4\u65b9\u6cd5\u7684\u5feb\u901f\u6307\u5357\uff1a \u8fc7\u6ee4\u6570\u7ec4\uff1a .filter() \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u901a\u8fc7\u6d4b\u8bd5\u7684\u5143\u7d20\u3002 const oddnumbers = [1, 2, 3, 4, 5, 6].filter(num =&gt; num % 2 !== 0); \/\/ [1, 3, 5] \u767b\u5f55\u540e\u590d\u5236 \u6620\u5c04\u6570\u7ec4\uff1a .map() \u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u3002 const doubled = [1, 2, 3, 4, 5].map(num =&gt; num * 2); \/\/ [2, 4, 6, 8, 10] \u767b\u5f55\u540e\u590d\u5236 \u51cf\u5c11\u6570\u7ec4\uff1a .reduce() \u5904\u7406\u6240\u6709\u5143\u7d20\u4ee5\u4ea7\u751f\u5355\u4e2a\u7ed3\u679c\u3002 const sum = [1, 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-3508","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3508","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=3508"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3508\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}