{"id":296441,"date":"2024-05-20T16:01:52","date_gmt":"2024-05-20T08:01:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/296441.html"},"modified":"2024-05-20T16:02:01","modified_gmt":"2024-05-20T08:02:01","slug":"%e5%89%8d%e7%ab%af-js-%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e5%88%9d%e5%a7%8b%e5%8c%96%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/296441.html","title":{"rendered":"\u524d\u7aef js \u4ee3\u7801\u5982\u4f55\u521d\u59cb\u5316\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27143924\/dbb71512-1115-440f-bd01-3c335efa0d6b.webp\" alt=\"\u524d\u7aef js \u4ee3\u7801\u5982\u4f55\u521d\u59cb\u5316\u6570\u7ec4\" \/><\/p>\n<p><p>\u524d\u7aefJavaScript\u4ee3\u7801\u521d\u59cb\u5316\u6570\u7ec4\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528\u5b57\u9762\u91cf\u3001\u8c03\u7528\u6570\u7ec4\u6784\u9020\u5668\u3001\u4f7f\u7528Array.of()\u3001\u4f7f\u7528Array.from()\u56db\u79cd\u65b9\u5f0f\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5b57\u9762\u91cf<\/strong>\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4ec5\u4ee3\u7801\u7b80\u6d01\u3001\u6613\u8bfb\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u9ad8\u3002\u5177\u4f53\u800c\u8a00\uff0c\u4f7f\u7528\u5b57\u9762\u91cf\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7f16\u5199\u4e00\u5bf9\u65b9\u62ec\u53f7<code>[]<\/code>\uff0c\u5e76\u5728\u5176\u5185\u90e8\u653e\u7f6e\u6570\u7ec4\u5143\u7d20\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4\u3002\u6bd4\u5982\uff0c<code>let fruits = [&#039;Apple&#039;, &#039;Banana&#039;, &#039;Cherry&#039;];<\/code>\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6570\u7ec4\u662f\u6700\u76f4\u63a5\u3001\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u5c06\u4e00\u5bf9\u65b9\u62ec\u53f7<code>[]<\/code>\u8d4b\u503c\u7ed9\u8fd9\u4e2a\u53d8\u91cf\uff0c\u5982\u679c\u9700\u8981\u5728\u6570\u7ec4\u4e2d\u521d\u59cb\u5316\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u65b9\u62ec\u53f7\u5185\u6dfb\u52a0\u5143\u7d20\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u9017\u53f7<code>,<\/code>\u5206\u9694\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\u53ef\u4ee5\u76f4\u63a5\u5199\u4e3a<code>let myArray = [];<\/code>\uff0c\u5982\u679c\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7279\u5b9a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u6bd4\u65b9\u8bf4\u4e00\u4e2a\u5305\u542b\u6570\u5b57<code>1<\/code>, <code>2<\/code>, <code>3<\/code>\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u5199\u4f5c<code>let numbers = [1, 2, 3];<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4ee3\u7801\u6e05\u6670\u6613\u61c2\uff0c\u800c\u4e14\u5728\u6240\u6709\u521d\u59cb\u5316\u6570\u7ec4\u7684\u65b9\u6cd5\u4e2d\uff0c\u6267\u884c\u6548\u7387\u6700\u9ad8\uff0c\u56e0\u4e3a\u5b83\u4e0d\u6d89\u53ca\u4efb\u4f55\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8c03\u7528\u6570\u7ec4\u6784\u9020\u5668\u521d\u59cb\u5316\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u8c03\u7528\u6570\u7ec4\u6784\u9020\u5668<code>new Array()<\/code>\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u521d\u59cb\u5316\u6570\u7ec4\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u6570\u7ec4\u957f\u5ea6\uff0c\u6216\u8005\u76f4\u63a5\u521d\u59cb\u5316\u6570\u7ec4\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>let arr = new Array(3);<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a3\u7684\u7a7a\u6570\u7ec4\uff0c\u800c<code>let fruits = new Array(&#039;Apple&#039;, &#039;Banana&#039;, &#039;Cherry&#039;);<\/code>\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u6c34\u679c\u540d\u79f0\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u4f7f\u7528\u5355\u4e00\u6570\u5b57\u53c2\u6570\u8c03\u7528\u6570\u7ec4\u6784\u9020\u5668\u65f6\uff0c\u8be5\u53c2\u6570\u5b9a\u4e49\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u800c\u975e\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u662f\u521d\u5b66\u8005\u5e38\u5e38\u9047\u5230\u7684\u4e00\u4e2a\u8bef\u533a\u3002\u5bf9\u4e8e\u591a\u4e2a\u53c2\u6570\u7684\u60c5\u51b5\uff0c\u5219\u76f4\u63a5\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f5c\u4e3a\u6570\u7ec4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Array.of()\u521d\u59cb\u5316\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>Array.of()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u521b\u5efa\u5177\u6709\u53ef\u53d8\u6570\u91cf\u53c2\u6570\u7684\u65b0\u6570\u7ec4\u5b9e\u4f8b\uff0c\u800c\u4e0d\u8003\u8651\u53c2\u6570\u7684\u6570\u91cf\u6216\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u8fd9\u4e00\u70b9\u4e0e\u6570\u7ec4\u6784\u9020\u5668<code>new Array()<\/code>\u7684\u884c\u4e3a\u4e0d\u540c\uff0c\u540e\u8005\u5728\u53ea\u6709\u4e00\u4e2a\u6570\u5b57\u53c2\u6570\u65f6\u5c06\u5176\u89c6\u4e3a\u6570\u7ec4\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>Array.of(7)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u53ea\u5305\u542b\u4e00\u4e2a\u5143\u7d20<code>7<\/code>\u7684\u6570\u7ec4\uff0c\u8fd9\u4e0e<code>new Array(7)<\/code>\u5f62\u6210\u5bf9\u6bd4\uff0c\u540e\u8005\u521b\u5efa\u4e86\u4e00\u4e2a\u957f\u5ea6\u4e3a7\u7684\u7a7a\u6570\u7ec4\u3002<code>Array.of(1, 2, 3)<\/code>\u5219\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5143\u7d20<code>1<\/code>, <code>2<\/code>, <code>3<\/code>\u7684\u6570\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u76f4\u63a5\u5c06\u4e00\u7ec4\u503c\u5feb\u901f\u8f6c\u6362\u6210\u6570\u7ec4\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Array.from()\u521d\u59cb\u5316\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>Array.from()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4ece\u7c7b\u4f3c\u6570\u7ec4\u6216\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u5b9e\u4f8b\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u4ece\u975e\u6570\u7ec4\u5bf9\u8c61\u521b\u5efa\u6570\u7ec4\u7684\u573a\u666f\uff0c\u6bd4\u5982\u5c06<code>NodeList<\/code>\u5bf9\u8c61\u6216\u8005\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p><code>Array.from(&#039;Hello&#039;)<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26<code>&#039;H&#039;<\/code>, <code>&#039;e&#039;<\/code>, <code>&#039;l&#039;<\/code>, <code>&#039;l&#039;<\/code>, <code>&#039;o&#039;<\/code>\u7684\u6570\u7ec4\u3002\u6b64\u5916\uff0c<code>Array.from()<\/code>\u8fd8\u63a5\u53d7\u4e00\u4e2a\u53ef\u9009\u7684\u6620\u5c04\u51fd\u6570\uff0c\u5141\u8bb8\u4f60\u5728\u751f\u6210\u6570\u7ec4\u7684\u540c\u65f6\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u8fd9\u4e3a\u6570\u7ec4\u7684\u521d\u59cb\u5316\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>Array.from([1, 2, 3], x =&gt; x + x);<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4<code>[2, 4, 6]<\/code>\uff0c\u8be5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u539f\u6570\u7ec4\u5143\u7d20\u7684\u4e24\u500d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6570\u7ec4\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\u7684\u590d\u5236\u548c\u8f6c\u5316\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u6570\u7ec4\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u524d\u7aefJS\u4ee3\u7801\u521d\u59cb\u5316\u6570\u7ec4\u7684\u56db\u79cd\u57fa\u672c\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u521d\u59cb\u5316\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u3001\u9ad8\u6548\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1.\u5982\u4f55\u4f7f\u7528 JavaScript \u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[]<\/code>\u6216<code>Array<\/code>\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">let array = []; \/\/ \u4f7f\u7528\u5b57\u9762\u91cf\u65b9\u5f0f\u521b\u5efa\u7a7a\u6570\u7ec4\n\/\/ \u6216\u8005\nlet array = new Array(); \/\/ \u4f7f\u7528Array\u6784\u9020\u51fd\u6570\u521b\u5efa\u7a7a\u6570\u7ec4\n<\/code><\/pre>\n<p><strong>2.\u5982\u4f55\u5728 JavaScript \u4e2d\u521d\u59cb\u5316\u4e00\u4e2a\u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5c06\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u7279\u5b9a\u7684\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Array.from<\/code>\u65b9\u6cd5\u6216\u5faa\u73af\u6765\u7ed9\u6570\u7ec4\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5c06\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u6307\u5b9a\u957f\u5ea6\u7684\u7a7a\u5b57\u7b26\u4e32\u6570\u7ec4\uff1a<\/p>\n<pre><code class=\"language-javascript\">let length = 5;\nlet array = Array.from({length}, () =&gt; &quot;&quot;); \/\/ \u4f7f\u7528Array.from\u65b9\u6cd5\n\/\/ \u6216\u8005\u4f7f\u7528\u5faa\u73af\nlet array = [];\nfor (let i = 0; i &lt; length; i++) {\n  array.push(&quot;&quot;);\n}\n<\/code><\/pre>\n<p><strong>3.\u5982\u4f55\u4f7f\u7528 JavaScript \u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u5c06\u9700\u8981\u7684\u5143\u7d20\u76f4\u63a5\u653e\u5728\u6570\u7ec4\u4e2d\uff0c\u6216\u4f7f\u7528<code>Array.of<\/code>\u6784\u9020\u51fd\u6570\u521b\u5efa\u5305\u542b\u6307\u5b9a\u5143\u7d20\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u548c\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\uff1a<\/p>\n<pre><code class=\"language-javascript\">let array = [1, &quot;two&quot;, 3, &quot;four&quot;]; \/\/ \u76f4\u63a5\u653e\u5165\u5143\u7d20\u6765\u521d\u59cb\u5316\u6570\u7ec4\n\/\/ \u6216\u8005\u4f7f\u7528Array.of\u65b9\u6cd5\nlet array = Array.of(1, &quot;two&quot;, 3, &quot;four&quot;);\n<\/code><\/pre>\n<p>\u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4e9b\u53ea\u662f\u521d\u59cb\u5316\u6570\u7ec4\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u6765\u521d\u59cb\u5316\u6570\u7ec4\u3002\u6587\u6863\u4e2d\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4f9b\u53c2\u8003\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u524d\u7aefJavaScript\u4ee3\u7801\u521d\u59cb\u5316\u6570\u7ec4\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528\u5b57\u9762\u91cf\u3001\u8c03\u7528\u6570\u7ec4\u6784\u9020\u5668\u3001\u4f7f\u7528Array.of()\u3001\u4f7f\u7528A [&hellip;]","protected":false},"author":3,"featured_media":296453,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296441"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=296441"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/296453"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=296441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=296441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=296441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}