{"id":4074,"date":"2024-11-10T15:27:26","date_gmt":"2024-11-10T07:27:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/4074\/"},"modified":"2024-11-10T15:27:26","modified_gmt":"2024-11-10T07:27:26","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-javascript-typescript-%e5%b0%86%e6%95%b0%e7%bb%84%e9%9b%86%e5%90%88%e7%9a%84%e5%89%8d%e5%87%a0%e5%85%83%e7%b4%a0%e6%8f%92%e5%85%a5%e5%8f%a6%e4%b8%80%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/4074\/","title":{"rendered":"\u5982\u4f55\u7528 JavaScript\/TypeScript \u5c06\u6570\u7ec4\u96c6\u5408\u7684\u524d\u51e0\u5143\u7d20\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408\u7684\u7279\u5b9a\u4f4d\u7f6e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122112783136.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528 JavaScript\/TypeScript \u5c06\u6570\u7ec4\u96c6\u5408\u7684\u524d\u51e0\u5143\u7d20\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408\u7684\u7279\u5b9a\u4f4d\u7f6e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528 JavaScript\/TypeScript \u5c06\u6570\u7ec4\u96c6\u5408\u7684\u524d\u51e0\u5143\u7d20\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408\u7684\u7279\u5b9a\u4f4d\u7f6e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 javascript\/ \u4e2d\u5c06\u6570\u7ec4\u96c6\u5408\u524d\u51e0\u5143\u7d20\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408\u7279\u5b9a\u4f4d\u7f6e<\/strong><\/p>\n<p>\u73b0\u6709\u4e00\u4e2a\u9700\u6c42\uff1a\u5c06\u6570\u7ec4\u96c6\u5408 arraryone \u4e2d\u7684\u524d x \u4f4d\u5143\u7d20\uff0c\u9010\u4e2a\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408 arrarytwo \u4e2d\u7684\u7b2c n \u4f4d\u3002\u539f\u6709\u65b9\u6848\u8fc7\u4e8e\u7e41\u7410\uff0c\u5982\u4f55\u7b80\u5316\u4ee3\u7801\uff1f<\/p>\n<p><strong>\u4f18\u5316\u65b9\u6848<\/strong><\/p>\n<p>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u65b9\u6848\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u524d n\uff0c\u63d2\u5165\u4f4d\u7f6e m\nvar n = 3, m = 2;\nvar a = [\"a1\", \"a2\", \"a3\", \"a4\", \"a5\"];\nvar b = [\"b1\", \"b2\", \"b3\", \"b4\", \"b5\"];\n\n\/\/ \u573a\u666f\u4e00\u3001\u5c06\u524d n \u4f5c\u4e3a\u6570\u7ec4\u63d2\u5165\u96c6\u5408\u4f4d\u7f6e m\nb.splice(m, 0, a.slice(0, n));\nconsole.log(b); \/\/ ['b1', 'b2', Array(3), 'b3', 'b4', 'b5']\n\n\/\/ \u573a\u666f\u4e8c\u3001\u5c06\u524d n \u9010\u4e2a\u63d2\u5165\u4f4d\u7f6e m\uff0c\u5e76\u4f9d\u6b21\u5f80\u540e\u7ee7\u7eed\u63d2\u5165\uff0c\u4f7f\u4e4b\u6210\u4e3a\u4e00\u4e2a\u540c\u7c7b\u578b\u6570\u7ec4\na.slice(0, n).forEach((item, i) =&gt; {\n  b.splice(m + i, 0, item);\n});\nconsole.log(b); \/\/ ['b1', 'b2', 'a1', 'a2', 'a3', 'b3', 'b4', 'b5']<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u65b9\u6848\u5229\u7528\u4e86 javascript \u7684 splice() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5728\u6570\u7ec4\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u3002\u6b64\u5916\uff0c\u6211\u4eec\u901a\u8fc7 slice() \u65b9\u6cd5\u83b7\u53d6\u4e86 arraryone \u4e2d\u7684\u524d x \u4f4d\u5143\u7d20\u3002<\/p>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>\u6b64\u4f18\u5316\u65b9\u6848\u9002\u7528\u4e8e\u5404\u79cd\u9700\u8981\u5728\u6570\u7ec4\u96c6\u5408\u4e2d\u63d2\u5165\u7279\u5b9a\u4f4d\u7f6e\u7684\u573a\u666f\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5728\u5206\u9875\u5217\u8868\u4e2d\u5c06\u65b0\u6570\u636e\u63d2\u5165\u6307\u5b9a\u9875\u7801<\/li>\n<li>\u5728\u6570\u636e\u5217\u8868\u4e2d\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u5e76\u63d2\u5165\u7279\u5b9a\u5143\u7d20<\/li>\n<li>\u5c06\u591a\u4e2a\u6570\u7ec4\u96c6\u5408\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u96c6\u5408\uff0c\u5e76\u4fdd\u6301\u5143\u7d20\u987a\u5e8f<\/li>\n<\/ul>\n<p><strong>\u5efa\u8bae<\/strong><\/p>\n<p>\u5efa\u8bae\u4f7f\u7528\u8be5\u4f18\u5316\u65b9\u6848\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u9ad8\u6548\uff0c\u5e76\u4e14\u9002\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u7528 JavaScript\/TypeScript \u5c06\u6570\u7ec4\u96c6\u5408\u7684\u524d\u51e0\u5143\u7d20\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408\u7684\u7279\u5b9a\u4f4d\u7f6e\uff1f\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>\u5728 javascript\/ \u4e2d\u5c06\u6570\u7ec4\u96c6\u5408\u524d\u51e0\u5143\u7d20\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408\u7279\u5b9a\u4f4d\u7f6e \u73b0\u6709\u4e00\u4e2a\u9700\u6c42\uff1a\u5c06\u6570\u7ec4\u96c6\u5408 arraryone \u4e2d\u7684\u524d x \u4f4d\u5143\u7d20\uff0c\u9010\u4e2a\u63d2\u5165\u53e6\u4e00\u4e2a\u6570\u7ec4\u96c6\u5408 arrarytwo \u4e2d\u7684\u7b2c n \u4f4d\u3002\u539f\u6709\u65b9\u6848\u8fc7\u4e8e\u7e41\u7410\uff0c\u5982\u4f55\u7b80\u5316\u4ee3\u7801\uff1f \u4f18\u5316\u65b9\u6848 \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u65b9\u6848\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u524d n\uff0c\u63d2\u5165\u4f4d\u7f6e m var n = 3, m = 2; var a = [&#8220;a1&#8221;, &#8220;a2&#8221;, &#8220;a3&#8221;, &#8220;a4&#8221;, &#8220;a5&#8221;]; var b = [&#8220;b1&#8221;, &#8220;b2&#8221;, &#8220;b3&#8221;, &#8220;b4&#8221;, &#8220;b5&#8221;]; \/\/ \u573a\u666f\u4e00\u3001\u5c06\u524d n \u4f5c\u4e3a\u6570\u7ec4\u63d2\u5165\u96c6\u5408\u4f4d\u7f6e m b.splice(m, 0, a.slice(0, n)); console.log(b); \/\/ [&#8216;b1&#8217;, &#8216;b2&#8217;, [&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-4074","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/4074","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=4074"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/4074\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=4074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=4074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=4074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}