{"id":1044,"date":"2022-04-17T23:03:51","date_gmt":"2022-04-17T15:03:51","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1044"},"modified":"2023-02-16T14:40:18","modified_gmt":"2023-02-16T06:40:18","slug":"7-array-de-jing-tai-fang-fa","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1044","title":{"rendered":"7 &#8211; Array \u7684\u9759\u6001\u65b9\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16502460725111.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/array-methods-in-js-33pe\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/array-methods-in-js-33pe<\/a><\/p>\n<h2><a id=\"array%E5%AE%9E%E7%94%A8%E7%9A%84%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Array \u5b9e\u7528\u7684\u9759\u6001\u65b9\u6cd5<\/h2>\n<p>\u5b58\u5728\u4e8e Array \u6784\u9020\u51fd\u6570\u4e0a\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3><a id=\"array-of\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Array.of<\/h3>\n<p>\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u521b\u5efa\u6570\u7ec4<\/p>\n<pre><code class=\"language-plain_text\">const x = Array.of(&quot;Parwinder&quot;, &quot;Bhagat&quot;);\nconsole.log(x); \/\/ [ 'Parwinder', 'Bhagat' ]\u2008\n\n\/\/ Example with the spread operator\n\nconst y = Array.of(...&quot;Parwinder&quot;);\nconsole.log(y); \/\/ [ 'P', 'a', 'r', 'w', 'i', 'n', 'd', 'e', 'r' ]\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f:<\/strong> \u6b64\u65b9\u6cd5\u4e0e Array \u6784\u9020\u51fd\u6570\u521b\u5efa\u6570\u7ec4\u662f\u6709\u533a\u522b\u7684\uff0c\u5f53\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u65f6 Array \u6784\u9020\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b  n  \u4e2a undefined  \u7684\u6570\u7ec4\uff0c\u800c Array.of \u53ea\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u90a3\u4e2a\u53c2\u6570\u7684\u6570\u7ec4\uff1a<\/p>\n<pre><code class=\"language-plain_text\">Array.of(7); \/\/ [7]\nArray(7); \/\/ array of 7 empty slots\n\nArray.of(1, 2, 3); \/\/ [1, 2, 3]\nArray(1, 2, 3);    \/\/ [1, 2, 3]\n<\/code><\/pre>\n<p>polyfill:<\/p>\n<pre><code class=\"language-plain_text\">if (!Array.of) {\n  Array.of = function () {\n    return Array.prototype.slice.call(arguments);\n  };\n}\n<\/code><\/pre>\n<h3><a id=\"array-from\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Array.from<\/h3>\n<p>\u6839\u636e\u4e00\u4e2a\u957f\u5ea6\u3001\u7c7b\u76ee\u3001\u7c7b\u6570\u7ec4\u6216\u8005\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u6765\u521b\u5efa\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-plain_text\">const x = Array.from({ length: 3 });\nconsole.log(x); \/\/ [ undefined, undefined, undefined ]\n\n\/\/ With items using a callback function as second parameter\nconst y = Array.from({ length: 3 }, function () {\n    return &quot;Hello&quot;;\n});\nconsole.log(y); \/\/ [ 'Hello', 'Hello', 'Hello' ]\n\n\n\/\/ With items using a callback function and arguments\nconst y = Array.from({ length: 3 }, function (value, index) {\n    return index;\n});\nconsole.log(y); \/\/ [ 0, 1, 2 ]\n<\/code><\/pre>\n<p>polyfill:<\/p>\n<pre><code class=\"language-plain_text\">if (!Array.from) {\n  Array.from = (function () {\n    var toStr = Object.prototype.toString;\n    var isCallable = function (fn) {\n      return typeof fn === 'function' || toStr.call(fn) === '[object Function]';\n    };\n    var toInteger = function (value) {\n      var number = Number(value);\n      if (isNaN(number)) {\n        return 0;\n      }\n      if (number === 0 || !isFinite(number)) {\n        return number;\n      }\n      return (number &gt; 0 ? 1 : -1) * Math.floor(Math.abs(number));\n    };\n    var maxSafeInteger = Math.pow(2, 53) - 1;\n    var toLength = function (value) {\n      var len = toInteger(value);\n      return Math.min(Math.max(len, 0), maxSafeInteger);\n    };\n\n    \/\/ The length property of the from method is 1.\n    return function from(arrayLike \/*, mapFn, thisArg *\/) {\n      \/\/ 1. Let C be the this value.\n      var C = this;\n\n      \/\/ 2. Let items be ToObject(arrayLike).\n      var items = Object(arrayLike);\n\n      \/\/ 3. ReturnIfAbrupt(items).\n      if (arrayLike == null) {\n        throw new TypeError(\n          'Array.from requires an array-like object - not null or undefined'\n        );\n      }\n\n      \/\/ 4. If mapfn is undefined, then let mapping be false.\n      var mapFn = arguments.length &gt; 1 ? arguments[1] : void undefined;\n      var T;\n      if (typeof mapFn !== 'undefined') {\n        \/\/ 5. else\n        \/\/ 5. a If IsCallable(mapfn) is false, throw a TypeError exception.\n        if (!isCallable(mapFn)) {\n          throw new TypeError(\n            'Array.from: when provided, the second argument must be a function'\n          );\n        }\n\n        \/\/ 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.\n        if (arguments.length &gt; 2) {\n          T = arguments[2];\n        }\n      }\n\n      \/\/ 10. Let lenValue be Get(items, &quot;length&quot;).\n      \/\/ 11. Let len be ToLength(lenValue).\n      var len = toLength(items.length);\n\n      \/\/ 13. If IsConstructor(C) is true, then\n      \/\/ 13. a. Let A be the result of calling the [[Construct]] internal method\n      \/\/ of C with an argument list containing the single item len.\n      \/\/ 14. a. Else, Let A be ArrayCreate(len).\n      var A = isCallable(C) ? Object(new C(len)) : new Array(len);\n\n      \/\/ 16. Let k be 0.\n      var k = 0;\n      \/\/ 17. Repeat, while k &lt; len\u2026 (also steps a - h)\n      var kValue;\n      while (k &lt; len) {\n        kValue = items[k];\n        if (mapFn) {\n          A[k] =\n            typeof T === 'undefined'\n              ? mapFn(kValue, k)\n              : mapFn.call(T, kValue, k);\n        } else {\n          A[k] = kValue;\n        }\n        k += 1;\n      }\n      \/\/ 18. Let putStatus be Put(A, &quot;length&quot;, len, true).\n      A.length = len;\n      \/\/ 20. Return A.\n      return A;\n    };\n  })();\n}\n<\/code><\/pre>\n<h3><a id=\"array-isarray\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Array.isArray()<\/h3>\n<p><strong>typeof<\/strong> \u4e00\u4e2a\u6570\u7ec4\u8fd4\u56de\u7684\u662f <strong>object<\/strong> \uff0c\u8fd9\u5c31\u662f\u4e3a\u4f55 isArray \u51fa\u73b0\u7684\u539f\u56e0\u3002<\/p>\n<pre><code class=\"language-plain_text\">const x = [&quot;Hello&quot;, &quot;World&quot;];\nconsole.log(Array.isArray(x)); \/\/ true\n\nconst y = 27;\nconsole.log(Array.isArray(y)); \/\/ false\n<\/code><\/pre>\n<p>polyfill:<\/p>\n<pre><code class=\"language-plain_text\">if (!Array.isArray) {\n    Array.isArray = function(arg) {\n      return Object.prototype.toString.call(arg) === '[object Array]';\n    };\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/array-methods-&#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,6],"class_list":["post-1044","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-array","tag-javascript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1044","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=1044"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1044\/revisions"}],"predecessor-version":[{"id":1046,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1044\/revisions\/1046"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}