{"id":1104,"date":"2022-04-26T10:17:19","date_gmt":"2022-04-26T02:17:19","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1104"},"modified":"2023-02-16T14:40:28","modified_gmt":"2023-02-16T06:40:28","slug":"9-array-de-shi-li-fang-fa-er","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1104","title":{"rendered":"9 &#8211;  Array \u7684\u5b9e\u4f8b\u65b9\u6cd5\u4e8c"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16509394704688.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/array-instance-methods-ii-d5l\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/array-instance-methods-ii-d5l<\/a><\/p>\n<p>\u5b9e\u4f8b\u65b9\u6cd5\u662f\u5b58\u5728\u4e8e Array \u7684 prototype \u4e0a\u7684\uff0c\u8fd9\u662f\u7b2c\u4e09\u7bc7\u5173\u4e8e Array \u65b9\u6cd5\u7684\u6587\u7ae0\u3002<\/p>\n<h3><a id=\"lastindexof\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>lastIndexOf<\/h3>\n<p>lastIndexOf \u662f\u4e3a\u4f60\u627e\u5230\u6307\u5b9a\u5143\u7d20\u6700\u540e\u51fa\u73b0\u7684\u4e0b\u6807\u4f4d\u7f6e\uff0c\u82e5\u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de -1 \u3002\u5b83\u8fd8\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53c2\u6570 <code>fromIndex<\/code> \u6765\u6307\u5b9a\u5411\u540e\u67e5\u627e\u7684\u4e0b\u6807\u3002<\/p>\n<pre><code class=\"language-plain_text\">const numbers = [1, 22, 123, 0, 15, 9, 88, 123, 0, 45];\nconsole.log(numbers.lastIndexOf(2)); \/\/ -1\nconsole.log(numbers.lastIndexOf(22)); \/\/ 1\nconsole.log(numbers.lastIndexOf(0)); \/\/ 8\nconsole.log(numbers.lastIndexOf(123, 4)); \/\/ 2\nconsole.log(numbers.lastIndexOf(0, 6)); \/\/ 3\nconsole.log(numbers.lastIndexOf(1, 1)); \/\/ 0\n<\/code><\/pre>\n<p>\u4e2a\u4eba\u6dfb\u52a0\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16509448838975.jpg\" alt=\"\" \/><\/p>\n<h3><a id=\"pop\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>pop<\/h3>\n<p>pop \u65b9\u6cd5\u79fb\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u3002<\/p>\n<pre><code class=\"language-plain_text\">const numbers = [1, 22, 123, 0, 15, 9, 88, 123, 0, 45];\nconsole.log(numbers.pop()); \/\/ 45\nconsole.log(numbers.pop()); \/\/ 0\nconsole.log(numbers.pop()); \/\/ 123\nconsole.log(numbers.pop()); \/\/ 88\n<\/code><\/pre>\n<h3><a id=\"reverse\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>reverse<\/h3>\n<p>reverse \u65b9\u6cd5\u539f\u5730\u7ffb\u8f6c\u6570\u7ec4\uff0c\u7136\u540e\u8fd4\u56de\u3002\u6ce8\u610f\uff1a\u6b64\u65b9\u6cd5\u66f4\u6539\u539f\u59cb\u6570\u7ec4\u7c7b\u4f3c sort\u3002<\/p>\n<pre><code class=\"language-plain_text\">const original = ['one', 'two', 'three'];\nconst reversed = original.reverse();\nconsole.log(reversed); \/\/ [&quot;three&quot;, &quot;two&quot;, &quot;one&quot;]\nconsole.log(original); \/\/ [&quot;three&quot;, &quot;two&quot;, &quot;one&quot;]\n<\/code><\/pre>\n<h3><a id=\"shift\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>shift<\/h3>\n<p>shift \u65b9\u6cd5\u7c7b\u4f3c pop\uff0cshift \u79fb\u51fa\u5934\u90e8\u7684\u5143\u7d20\uff0c\u800c pop \u662f\u79fb\u51fa\u5c3e\u90e8\u5143\u7d20\u3002\u5b83\u4eec\u90fd\u8fd4\u56de\u79fb\u9664\u7684\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-plain_text\">const original = [1, 2, 3]\nconst firstRemoved = original.shift();\nconsole.log(original); \/\/ [2, 3]\nconsole.log(firstRemoved); \/\/ 1\n<\/code><\/pre>\n<h3><a id=\"some\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>some<\/h3>\n<p>some \u65b9\u6cd5\u7528\u6765\u68c0\u6d4b\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u56de\u8c03\u51fd\u6570\u6216\u6d4b\u8bd5\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-plain_text\">const array = [1, 2, 3, 4, 5];\nconst even = (element) =&gt; element % 2 === 0; \/\/ checks whether an element is even\nconst greaterThanTen = (element) =&gt; element &gt; 10; \/\/ checks whether an element is greater than 10\nconsole.log(array.some(even)); \/\/ true\nconsole.log(array.some(greaterThanTen)); \/\/ false\n<\/code><\/pre>\n<h3><a id=\"sort\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>sort<\/h3>\n<p>sort \u65b9\u6cd5\u9ed8\u8ba4\u6309\u5347\u5e8f\uff08\u5143\u7d20\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u6bd4\u8f83\u5b83\u4eec\u7684 UTF-16 \u4ee3\u7801\u5355\u5143\u503c\u7684\u5e8f\u5217\uff09\u6392\u5217\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-plain_text\">const months = ['March', 'Jan', 'Feb', 'Dec'];\nmonths.sort();\nconsole.log(months); \/\/ [&quot;Dec&quot;, &quot;Feb&quot;, &quot;Jan&quot;, &quot;March&quot;]\n\nconst numbers = [1, 30, 4, 21, 100000];\narray1.sort();\nconsole.log(array1); \/\/ [1, 100000, 21, 30, 4]\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50 100000 \u600e\u4e48\u6392\u5230 21 \u524d\u9762\u7684\uff0c\u90a3\u662f\u56e0\u4e3a\u5728\u6392\u5e8f\u524d\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7136\u540e\u518d\u6bd4\u8f83\u3002<\/p>\n<p>sort \u65b9\u6cd5\u5141\u8bb8\u4f20\u9012\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u81ea\u5b9a\u4e49\u6bd4\u8f83\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-plain_text\">const numbers = [1, 30, 4, 21, 100000];\nnumbers.sort(function(a, b) {\n  return a - b;\n});\nconsole.log(numbers); \/\/ [ 1, 4, 21, 30, 100000 ]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u5c31\u8fd4\u56de\u4e86\u4f60\u60f3\u8981\u7684\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/p>\n<h3><a id=\"tostring\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>toString<\/h3>\n<p>toString \u8fd4\u56de\u6570\u7ec4\u5143\u7d20\u7ec4\u5408\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-plain_text\">const sample = [1, 2, &quot;Parwinder&quot;, true, &quot;Hello&quot;, 78];\nconsole.log(sample.toString()); \/\/ 1,2,Parwinder,true,Hello,78\n<\/code><\/pre>\n<h3><a id=\"unshift\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>unshift<\/h3>\n<p>unshift \u7c7b\u4f3c push \u65b9\u6cd5\uff0cpush \u5411\u6570\u7ec4\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u800c unshift \u5411\u6570\u7ec4\u5934\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5b83\u4eec\u90fd\u8fd4\u56de\u6570\u7ec4\u7684\u957f\u5ea6\u800c\u4e0d\u662f\u65b0\u7684\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-plain_text\">const numbers = [1, 2, 3];\nconsole.log(numbers.unshift(4, 5, 6)); \/\/ 6\nconsole.log(numbers); \/\/ [ 4, 5, 6, 1, 2, 3 ]\n<\/code><\/pre>\n<p>\u5230\u6b64\u5c31\u662f\u6211\u8981\u4ecb\u7ecd\u7684\u4e00\u4e9b\u5e38\u7528\u6570\u7ec4\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u8bf7\u67e5\u9605\uff1a<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\" target=\"_blank\" rel=\"noopener\">MDN<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/array-instance&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[96],"class_list":["post-1104","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-array"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1104"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1104\/revisions"}],"predecessor-version":[{"id":1105,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1104\/revisions\/1105"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}