{"id":28,"date":"2023-06-01T10:02:00","date_gmt":"2023-06-01T02:02:00","guid":{"rendered":"https:\/\/blog.pixris.online\/?p=28"},"modified":"2025-05-28T12:05:42","modified_gmt":"2025-05-28T04:05:42","slug":"javascript%e6%95%b0%e7%bb%84%e5%ae%8c%e5%85%a8%e6%8c%87%e5%8d%97%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e6%96%b9%e6%b3%95%e5%88%b0%e9%ab%98%e9%98%b6%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/blog.pixris.online\/index.php\/2023\/06\/01\/28\/","title":{"rendered":"JavaScript\u6570\u7ec4\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u57fa\u7840\u65b9\u6cd5\u5230\u9ad8\u9636\u6280\u5de7"},"content":{"rendered":"\n<p>\u6570\u7ec4\u662fJavaScript\u5f00\u53d1\u4e2d\u6700\u57fa\u7840\u5374\u6700\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u636e\u5b58\u50a8\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u8f6c\u6362\uff0c\u6570\u7ec4\u65b9\u6cd5\u90fd\u80fd\u4e3a\u6211\u4eec\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u7840\u6838\u5fc3\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u589e\u5220\u64cd\u4f5c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5c3e\u90e8\u64cd\u4f5c\nconst fruits = &#91;'apple'];\nfruits.push('banana');      \/\/ &#91;'apple', 'banana']\nconst last = fruits.pop();  \/\/ 'banana'\n\n\/\/ \u5934\u90e8\u64cd\u4f5c\nfruits.unshift('orange');   \/\/ &#91;'orange', 'apple']\nconst first = fruits.shift(); \/\/ 'orange'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4e07\u80fd\u65b9\u6cd5splice<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const numbers = &#91;1, 2, 3, 4, 5];\n\n\/\/ \u5220\u9664\u4e24\u4e2a\u5143\u7d20\uff08\u7d22\u5f152\u5f00\u59cb\uff09\nnumbers.splice(2, 2); \/\/ \u8fd4\u56de &#91;3,4]\uff0c\u539f\u6570\u7ec4\u53d8\u4e3a &#91;1,2,5]\n\n\/\/ \u66ff\u6362\u64cd\u4f5c\nnumbers.splice(1, 0, 'a', 'b'); \/\/ &#91;1, 'a', 'b', 2, 5]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u8fed\u4ee3\u65b9\u6cd5\u5168\u5bb6\u6876<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u904d\u5386\u4e09\u5251\u5ba2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ forEach\uff1a\u5355\u7eaf\u904d\u5386\n&#91;1, 2, 3].forEach(num =&gt; console.log(num * 2));\n\n\/\/ map\uff1a\u6570\u636e\u8f6c\u6362\nconst doubled = &#91;1, 2, 3].map(num =&gt; num * 2); \/\/ &#91;2,4,6]\n\n\/\/ filter\uff1a\u6570\u636e\u7b5b\u9009\nconst evens = &#91;1, 2, 3, 4].filter(n =&gt; n%2 === 0); \/\/ &#91;2,4]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u9ad8\u7ea7\u8fed\u4ee3\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ reduce\u4e07\u80fd\u805a\u5408\nconst sum = &#91;1, 2, 3].reduce((acc, cur) =&gt; acc + cur, 0); \/\/ 6\n\n\/\/ \u67e5\u627e\u7c7b\u65b9\u6cd5\nconst users = &#91;\n  {id: 1, name: 'Alice'},\n  {id: 2, name: 'Bob'}\n];\n\nusers.find(user =&gt; user.id === 2); \/\/ {id:2...}\nusers.some(user =&gt; user.age &gt; 18); \/\/ \u5e03\u5c14\u68c0\u6d4b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u9ad8\u9636\u5e94\u7528\u6280\u5de7<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6570\u7ec4\u53bb\u91cd\u56db\u91cd\u594f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Set\u65b9\u6cd5\uff08ES6\uff09\nconst unique = &#91;...new Set(&#91;1,2,2,3])]; \/\/ &#91;1,2,3]\n\n\/\/ filter\u7d22\u5f15\u6cd5\n&#91;1,2,2,3].filter((v,i) =&gt; i === arr.indexOf(v));\n\n\/\/ reduce\u5b9e\u73b0\n&#91;1,2,2,3].reduce((acc, cur) =&gt; \n  acc.includes(cur) ? acc : &#91;...acc, cur], &#91;]);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6570\u7ec4\u8f6c\u6362\u6280\u5de7<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u7c7b\u6570\u7ec4\u8f6c\u6362\nconst nodeList = document.querySelectorAll('div');\nconst realArray = Array.from(nodeList);\n\n\/\/ \u89e3\u6784\u5999\u7528\nconst &#91;first, ...rest] = &#91;1,2,3,4]; \/\/ first=1, rest=&#91;2,3,4]\n\n\/\/ \u6241\u5e73\u5316\u5904\u7406\nconst deepArray = &#91;1, &#91;2, &#91;3]]];\ndeepArray.flat(Infinity); \/\/ &#91;1,2,3]\uff08ES2019\uff09<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6027\u80fd\u4f18\u5316\u6307\u5357<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5faa\u73af\u9677\u9631<\/strong><br>\u907f\u514d\u5728\u904d\u5386\u65f6\u4fee\u6539\u6570\u7ec4\u957f\u5ea6\uff0c\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u8df3\u8fc7\u5143\u7d20<\/li>\n\n\n\n<li><strong>\u65b9\u6cd5\u9009\u62e9<\/strong><br>\u6839\u636e\u9700\u6c42\u9009\u62e9\u6700\u4f18\u65b9\u6cd5\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u9700\u68c0\u6d4b\u5b58\u5728\u6027\u7528<code>some()<\/code>\u800c\u975e<code>filter()<\/code><\/li>\n\n\n\n<li>\u67e5\u627e\u5355\u4e2a\u5143\u7d20\u7528<code>find()<\/code>\u800c\u4e0d\u662f<code>filter()[0]<\/code><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5927\u6570\u636e\u5904\u7406<\/strong><br>\u8d85\u8fc710\u4e07\u6761\u6570\u636e\u65f6\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u4ee3\u66ff<code>forEach<\/code><\/li>\n\n\n\n<li>\u907f\u514d\u94fe\u5f0f\u8c03\u7528\u591a\u6b21\u904d\u5386<\/li>\n\n\n\n<li>\u91c7\u7528Web Worker\u8fdb\u884c\u5206\u5757\u5904\u7406<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u51b7\u95e8\u4f46\u5b9e\u7528\u7684\u65b9\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6570\u7ec4\u9a8c\u8bc1\nArray.isArray(&#91;]); \/\/ true\uff08\u6bd4typeof\u66f4\u53ef\u9760\uff09\n\n\/\/ \u521b\u5efa\u5e8f\u5217\u6570\u7ec4\nArray.from({length:5}, (_,i) =&gt; i+1); \/\/ &#91;1,2,3,4,5]\n\n\/\/ \u5b89\u5168\u5408\u5e76\u6570\u7ec4\nconst merged = &#91;...arr1, ...arr2]; \/\/ \u66ff\u4ee3concat\n\n\/\/ \u7a7a\u4f4d\u5904\u7406\nconst sparse = &#91;1,,3];\nsparse.flat(0); \/\/ &#91;1,3]\uff08ES6\u5904\u7406\u7a7a\u4f4d\uff09<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\u4e0e\u601d\u7ef4\u5bfc\u56fe<\/h2>\n\n\n\n<p>\u638c\u63e1\u6570\u7ec4\u65b9\u6cd5\u9700\u8981\u7406\u89e3\u4e09\u4e2a\u6838\u5fc3\u7ef4\u5ea6\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u662f\u5426\u4fee\u6539\u539f\u6570\u7ec4<\/strong>\uff08\u5982push vs concat\uff09<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u503c\u7c7b\u578b<\/strong>\uff08\u6570\u7ec4\/\u5143\u7d20\/undefined\uff09<\/li>\n\n\n\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff08\u8f6c\u6362\/\u7b5b\u9009\/\u805a\u5408\uff09<\/li>\n<\/ol>\n\n\n\n<p>\u5efa\u8bae\u901a\u8fc7\u5b9e\u9645\u9879\u76ee\u7ec3\u4e60\u8fd9\u4e9b\u65b9\u6cd5\u7ec4\u5408\u5e94\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>map+filter<\/code>\u8fdb\u884c\u6570\u636e\u6e05\u6d17<\/li>\n\n\n\n<li>\u5229\u7528<code>reduce<\/code>\u5b9e\u73b0\u590d\u6742\u6570\u636e\u8f6c\u6362<\/li>\n\n\n\n<li>\u7ed3\u5408<code>findIndex+splice<\/code>\u8fdb\u884c\u6761\u4ef6\u5220\u9664<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6269\u5c55\u5b66\u4e60<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\">MDN Array\u6587\u6863<\/a><\/li>\n\n\n\n<li>\u300aJavaScript\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u300b\u7b2c6\u7ae0<\/li>\n\n\n\n<li>Lodash\u5e93\u7684\u6570\u7ec4\u5de5\u5177\u65b9\u6cd5<\/li>\n<\/ul>\n\n\n\n<p>\u638c\u63e1\u8fd9\u4e9b\u6570\u7ec4\u6280\u5de7\uff0c\u60a8\u5c06\u80fd\u5199\u51fa\u66f4\u7b80\u6d01\u3001\u9ad8\u6548\u7684JavaScript\u4ee3\u7801\u3002\u6709\u4efb\u4f55\u7591\u95ee\u6216\u8865\u5145\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u4ea4\u6d41\u8ba8\u8bba\uff01<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u7ec4\u662fJavaScript\u5f00\u53d1\u4e2d\u6700\u57fa\u7840\u5374\u6700\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u636e\u5b58\u50a8\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u8f6c\u6362\uff0c\u6570\u7ec4\u65b9\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":36,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,18],"tags":[20,10,21],"class_list":["post-28","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-js","category-18","tag-javascript","tag-10","tag-21"],"_links":{"self":[{"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":3,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/posts\/28\/revisions\/31"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/media\/36"}],"wp:attachment":[{"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pixris.online\/index.php\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}