{"id":10667,"date":"2022-12-28T21:48:06","date_gmt":"2022-12-28T13:48:06","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10667"},"modified":"2022-12-28T21:48:06","modified_gmt":"2022-12-28T13:48:06","slug":"js-array-copywithin","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2022\/12\/js-array-copywithin\/","title":{"rendered":"JS\u6570\u7ec4\u7684copyWithin()\u8bed\u6cd5\u6211\u770b\u4e86\u597d\u51e0\u904d\u624d\u61c2"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10667\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10667<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202212-copywithin.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/copywithin-cover.png\" width=\"480\" height=\"306\" alt=\"copywithin\u5c01\u9762\u56fe\" class=\"alignright thumb clip size-medium\" \/><\/p>\n<h3>\u4e00\u3001copyWithin()\u5bf9\u6bd4slice()<\/h3>\n<p>JavaScript\u4e2d\u6570\u7ec4\u7684copyWithin()\u548cslice()\u65b9\u6cd5\u4f5c\u7528\u90fd\u662f\u590d\u5236\u6570\u7ec4\uff0c\u4e14\u90fd\u662f\u6d45\u590d\u5236\u3002<\/p>\n<p><strong>\u533a\u522b\u4e4b\u4e00\u5728\u4e8e<\/strong><\/p>\n<p><code>copyWithin()<\/code>\u662f\u5c06\u6570\u7ec4\u4e2d\u7684\u4e00\u90e8\u5206\u590d\u5236\u5e76\u66ff\u6362\u53e6\u5916\u4e00\u90e8\u5206\uff0c\u603b\u957f\u5ea6\u662f\u4e0d\u53d8\u7684\uff0c\u800c<code>slice()<\/code>\u65b9\u6cd5\u53ea\u590d\u5236\uff0c\u4e0d\u66ff\u6362\uff0c\u8fd4\u56de\u7684\u6570\u7ec4\u957f\u5ea6\u662f\u7531\u590d\u5236\u7684\u6570\u7ec4\u9879\u76ee\u4e2a\u6570\u51b3\u5b9a\u7684\u3002<\/p>\n<p>\u770b\u4e00\u4e2a\u533a\u522b\uff0c\u5df2\u77e5\u6570\u7ec4 <code>arr<\/code> \u4e3a <code>[1, 'A', '\u7532', 'I']<\/code>\uff0c\u5219\u5206\u522b\u6267\u884c <code>arr.copyWithin(2, 3)<\/code> \u548c <code>arr.slice(2, 3)<\/code> \u4f1a\u6709\u5982\u4e0b\u6240\u793a\u7684\u4e0d\u540c\u7ed3\u679c\u3002<\/p>\n<pre>arr = [1, 'A', '\u7532', 'I'];\r\nconsole.log(arr.copyWithin(2, 3));\r\n\/\/ \u7ed3\u679c\u662f\uff1a\u00a0[1, 'A', 'I', 'I']<\/pre>\n<pre>arr = [1, 'A', '\u7532', 'I'];\r\nconsole.log(arr.slice(2, 3));\r\n\/\/ \u7ed3\u679c\u662f\uff1a\u00a0['\u7532']<\/pre>\n<p>\u5728\u63a7\u5236\u53f0\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/2022-12-27_173859.png\" width=\"478\" height=\"318\" alt=\"\u4e24\u4e2a\u6570\u7ec4\u590d\u5236\u65b9\u6cd5\u8fd0\u884c\u7ed3\u679c\u5bf9\u6bd4\" class=\"alignnone size-medium\" \/><\/p>\n<p><strong>\u533a\u522b\u4e4b\u4e8c\u5728\u4e8e<\/strong><\/p>\n<p><code>copyWithin()<\/code>\u65b9\u6cd5\u4f1a\u6539\u53d8\u539f\u59cb\u7684\u6570\u7ec4\uff0c\u800c<code>slice()<\/code>\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>arr = [1, 'A', '\u7532', 'I'];\r\narr.copyWithin(2, 3);\r\nconsole.log(arr);\r\n\/\/ \u7ed3\u679c\u662f\uff1a\u00a0[1, 'A', 'I', 'I']<\/pre>\n<pre>arr = [1, 'A', '\u7532', 'I'];\r\narr.slice(2, 3);\r\nconsole.log(arr);\r\n\/\/ \u7ed3\u679c\u662f\uff1a[1, 'A', '\u7532', 'I']<\/pre>\n<p>\u63a7\u5236\u53f0\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/2022-12-27_174953.png\" width=\"405\" height=\"333\" alt=\"\u5f71\u54cd\u539f\u6570\u7ec4\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>copyWithin()<\/code>\u65b9\u6cd5\u6267\u884c\u540e\uff0c\u539f\u59cb\u7684arr\u6570\u7ec4\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u53d8\u6210\u4e86\u590d\u5236\u540e\u7684\u6570\u7ec4\u5185\u5bb9\u3002<\/p>\n<p><strong>\u533a\u522b\u4e4b\u4e09\u5728\u4e8e<\/strong><\/p>\n<p><code>slice()<\/code>\u590d\u5236\u5bf9\u4e8e\u5b57\u7b26\u4e32\u4e5f\u662f\u6709\u6548\u7684\uff0c\u4f46\u662f<code>copyWithin()<\/code>\u65b9\u6cd5\u5374\u4e0d\u652f\u6301\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u8fd4\u56de\u503c\u662f'angx'\r\n('zhangxinxu').slice(2, 6)<\/pre>\n<p>\u4f46\u662f\u4e0b\u9762\u7684\u8bed\u53e5\u5c31\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre>\/\/ \u4f1a\u65e0\u60c5\u62a5\u9519\r\n('zhangxinxu').copyWithin(2, 6)<\/pre>\n<h3>\u4e8c\u3001copyWithin()\u7684\u8bed\u6cd5<\/h3>\n<p><code>copyWithin()<\/code>\u7684\u8bed\u6cd5\u5f62\u5f0f\u4e0a\u4e0d\u7b97\u590d\u6742\uff0c\u4f46\u662f\u7406\u89e3\u8d77\u6765\u6709\u4e9b\u8d39\u8111\u7ec6\u80de\uff0c\u6211\u4e5f\u662f\u770b\u4e86\u597d\u51e0\u904d\u624d\u5f04\u660e\u767d\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>Array.prototype.copyWithin(index, start, end)<\/pre>\n<p>\u8868\u793a\u590d\u5236start-end\u7684\u5185\u5bb9\uff0c\u5e76\u4eceindex\u5f00\u59cb\u66ff\u6362\u3002<\/p>\n<p><strong>\u4f7f\u7528\u793a\u610f\uff1a<\/strong><\/p>\n<pre>[].copyWithin()\r\n[].copyWithin(index)\r\n[].copyWithin(index, start)\r\n[].copyWithin(index, start, end)<\/pre>\n<h4>\u53c2\u6570<\/h4>\n<dl>\n<dt><strong>index<\/strong><\/dt>\n<dd>\u590d\u5236\u7684\u6570\u7ec4\u9879\u5f00\u59cb\u66ff\u6362\u7684\u8d77\u59cb\u7d22\u5f15\u503c\u3002<\/dd>\n<dt><strong>start<\/strong><\/dt>\n<dd>\u590d\u5236\u7684\u6570\u7ec4\u9879\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u5305\u542b\uff09\u3002<\/dd>\n<dt><strong>end<\/strong><\/dt>\n<dd>\u590d\u5236\u7684\u6570\u7ec4\u9879\u7684\u7ed3\u675f\u4f4d\u7f6e\uff08\u4e0d\u5305\u542b\uff09\u3002<\/dd>\n<\/dl>\n<p><strong>\u4e3e\u4f8b\u8bf4\u660e<\/strong><\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>arr = [1, 'A', '\u7532', 'I', '\u4e00'];\r\narr.copyWithin(2, 3, 4);<\/pre>\n<p>\u6b64\u65f6\uff0cstart=3\uff0cend=4\uff0c\u8868\u793a\u590d\u5236\u6570\u7ec4\u7684arr[3]\uff08\u5305\u542b\uff09\u5230arr[4]\u9879\uff08\u4e0d\u5305\u542b\uff09\uff0c\u56e0\u6b64\u590d\u5236\u7684\u503c\u662f arr[3]\uff0c\u4e5f\u5c31\u662f &#8216;I&#8217;\u3002<br \/>\nindex=2\uff0c\u5219\u8868\u793a\u590d\u5236\u7684\u503c\u4ecearr[2]\u8fd9\u91cc\u5f00\u59cb\u66ff\u6362\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/copyWithin-explain-s.png\" width=\"360\" height=\"242\" alt=\"copyWithin\u539f\u7406\u793a\u610f\" class=\"alignnone imgpad size-medium\" \/><\/p>\n<p>\u4e8e\u662f\u590d\u5236\u7684\u7ed3\u679c\u5c31\u662f\uff1a[1, &#8216;A&#8217;, &#8216;I&#8217;, &#8216;I&#8217;, &#8216;\u4e00&#8217;]<\/p>\n<h4>\u53c2\u6570\u7ec6\u8282<\/h4>\n<p><code>copyWithin()<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\u7ec6\u8282\u6bd4\u8f83\u591a\uff0c\u6bd5\u7adf\u6570\u503c\u6709\u5927\u6709\u5c0f\uff0c\u6709\u6b63\u6709\u8d1f\uff0c\u60c5\u51b5\u4e0d\u540c\uff0c\u89c4\u5219\u4e5f\u4f1a\u4e0d\u540c\u3002<\/p>\n<p>\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<p><strong>1. index\u53c2\u6570<\/strong><\/p>\n<ul>\n<li><code>index<\/code>\u53c2\u6570\u4e0d\u8bbe\u7f6e\u4e5f\u662f\u5408\u6cd5\u7684\uff0c\u4e0d\u8fc7\u4f60\u4e0d\u8bbe\u7f6e\u8be5\u53c2\u6570\u6570\u7ec4\u5c31\u4f1a\u539f\u8def\u590d\u5236\u8fd4\u56de\uff0c\u7c7b\u4f3c\u4e8e\u8bbe\u7f6e\u4e3a 0\uff0c\u8fd9\u6837\u5904\u7406\u53ea\u4f1a\u6d6a\u8d39\u6027\u80fd\uff0c\u6ca1\u6709\u4efb\u4f55\u5b9e\u9645\u610f\u4e49\u3002\n<p>\u56e0\u6b64\uff0c<code>index<\/code>\u53c2\u6570\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5fc5\u987b\u53c2\u6570\u3002<\/li>\n<li>\u5982\u679cindex\u662f\u5c0f\u6570\uff0c\u5219\u4f1a\u6309\u7167\u6574\u6570\u5904\u7406\uff0c\u5e76\u4e14\u662f\u5411\u4e0b\u53d6\u6574\u3002\u4f8b\u5982\uff1a\n<pre>\/\/ \u8fd4\u56de\u503c\u662f true\r\n[1, 2, 3].copyWithin(1.5).join() == [1, 2, 3].copyWithin(1).join()\r\n\/\/ copyWithin(1.5)\u6267\u884c\u7ed3\u679c\u662f\uff1a[1, 1, 2]<\/pre>\n<\/li>\n<li>\u5982\u679cindex\u662f\u8d1f\u6570\u3002\n<ul style=\"list-style-type: circle;\">\n<li>\u5982\u679cindex\u8d1f\u6570\u7684\u7edd\u5bf9\u503c\u5f88\u5927\uff0c\u751a\u81f3\u6bd4\u6570\u7ec4\u7684\u957f\u5ea6\u8fd8\u8981\u5927\uff0c\u5219\u6b64\u65f6 index \u5f53\u4f5c 0 \u5904\u7406\u3002\u4f8b\u5982\uff1a\n<pre>\/\/ \u8fd4\u56de\u503c\u662f ['zhang', 'xin', 'xu']\r\n\/\/ \u7b49\u540c\u4e8e copyWithin(0)\r\n['zhang', 'xin', 'xu'].copyWithin(-999)<\/pre>\n<\/li>\n<li>\u5426\u5219\uff0c\u6700\u7ec8\u7684 index \u503c\u7b49\u4e8e index + arr.length\uff0c\u6bd4\u65b9\u8bf4 <code>[1, 2, 3].copyWithin(-1)<\/code> \u5c31\u7b49\u540c\u4e8e <code>[1, 2, 3].copyWithin(2)<\/code>\uff1b\n<pre>\/\/ \u7ed3\u679c\u662f[1, 2, 1]\r\n[1, 2, 3].copyWithin(-1);\r\n\/\/ \u7ed3\u679c\u540c\u6837\u662f[1, 2, 1]\r\n[1, 2, 3].copyWithin(2);<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/2022-12-28_162421.png\" width=\"331\" height=\"189\" alt=\"\u8d1f\u6570 index \u503c\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/li>\n<\/ul>\n<\/li>\n<li>\u5982\u679cindex\u7684\u503c\u8d85\u51fa\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\u8303\u56f4\uff0c\u5219\u4e0d\u4f1a\u6709\u4efb\u4f55\u5185\u5bb9\u88ab\u590d\u5236\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u4e0b\uff0c\u6ca1\u6709\u4efb\u4f55\u5185\u5bb9\u590d\u5236\u548c\u6240\u6709\u5185\u5bb9\u90fd\u590d\u5236\u662f\u6709\u533a\u522b\u7684\uff0c\u867d\u7136\u770b\u8d77\u6765\u8fd4\u56de\u7684\u90fd\u50cf\u662f\u539f\u6570\u7ec4\uff0c\u4f46\u5176\u5b9e\u5e76\u4e0d\u662f\uff0c\u6027\u80fd\u8fd9\u5757\u662f\u6709\u7740\u660e\u663e\u7684\u5dee\u5f02\u7684\u3002\n<p>\u6211\u4eec\u53ef\u4ee5\u7528\u975e\u5e38\u7b80\u5355\u7684\u65b9\u6cd5\u6d4b\u8bd5\u4e0b\uff0c\u4f8b\u5982\u6211\u4eec\u6784\u9020\u4e00\u4e2a99\u4e07\u4e2a\u9879\u76ee\u7684\u6570\u7ec4\u8fdb\u884c\u590d\u5236\u5904\u7406\uff1a<\/p>\n<pre>console.time('start');\r\narr = new Array(999999);\r\nconsole.timeLog('start');\r\narr.copyWithin(0);\r\nconsole.timeLog('start');\r\narr.copyWithin(1000000);\r\nconsole.timeEnd('start');<\/pre>\n<p>\u6211\u4eec\u5728\u63a7\u5236\u53f0\u8dd1\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u901a\u8fc7\u89c2\u5bdf\u6267\u884c\u65f6\u95f4\u5c31\u4f1a\u53d1\u73b0\u6267\u884ccopyWithin(0)\u7528\u4e86\u8fd170ms\uff0c\u800c\u6267\u884ccopyWithin(1000000)\u7684\u7528\u65f6\u51e0\u4e4e\u5c31\u662f0.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/2022-12-28_185845.png\" width=\"618\" height=\"363\" alt=\"\u8fd0\u884c\u65f6\u95f4\u5bf9\u6bd4\" class=\"alignnone size-medium\" \/><\/p>\n<\/li>\n<li>\u5982\u679c index \u7684\u4f4d\u7f6e\u5728 start \u4e4b\u540e\uff0c\u5219\u590d\u5236\u7684\u5185\u5bb9\u7c98\u8d34\u5230\u6570\u7ec4\u957f\u5ea6\u7684\u5c3e\u90e8\u5c31\u505c\u6b62\u4e86\uff0c\u4e5f\u5c31\u662f\u4e0d\u4f1a\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\u7684\uff1a\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u7ed3\u679c\u662f [1, 'A', '\u7532', 'A', '\u7532']\r\n[1, 'A', '\u7532', 'I', '\u4e00'].copyWithin(3, 1);<\/pre>\n<\/li>\n<\/ul>\n<p><strong>2. start\u53c2\u6570<\/strong><\/p>\n<ul>\n<li>start\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u5219\u5f53\u4f5c0\u5904\u7406\uff0c\u8868\u793a\u6570\u7ec4\u4ece\u5934\u590d\u5236\u5230\u5c3e\u90e8\uff08index\u5c0f\u4e8e\u6570\u503c\u957f\u5ea6\u7684\u524d\u63d0\u4e0b\uff09\uff1b<\/li>\n<li>start\u5982\u679c\u662f\u5c0f\u6570\uff0c\u4f1a\u5411\u4e0b\u53d6\u6574\u5904\u7406\u3002\u4f8b\u5982<code>[].copyWithin(0, 1.9999)<\/code>\u7b49\u540c\u4e8e <code>[].copyWithin(0, 1)<\/code>\uff1b<\/li>\n<li>start\u5982\u679c\u662f\u8d1f\u6570\uff0c\u5219\u4ece\u6570\u7ec4\u7684\u540e\u9762\u5f00\u59cb\u53d6\u503c\uff0c\u7b49\u540c\u4e8e start + arr.length\uff1b<\/li>\n<li>start\u5982\u679c\u662f\u8d1f\u6570\uff0c\u540c\u65f6\u7edd\u5bf9\u503c\u5c0f\u4e8e -1 * arr.length\uff0c\u5219\u5f53\u4f5c 0 \u5904\u7406\u3002<\/li>\n<li>start\u5982\u679c\u8d85\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\u8303\u56f4\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u590d\u5236\u3002<\/li>\n<\/ul>\n<p><strong>2. end\u53c2\u6570<\/strong><\/p>\n<ul>\n<li>end\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u4e0d\u8bbe\u7f6e\uff0c\u5219\u5f53\u4f5c<code>array.length<\/code>\u5904\u7406\uff0c\u8868\u793a\u6570\u7ec4\u590d\u5236\u5230\u5c3e\u90e8\uff1b<\/li>\n<li>end\u5982\u679c\u662f\u5c0f\u6570\uff0c\u4f1a\u5411\u4e0b\u53d6\u6574\u5904\u7406\u3002\u4f8b\u5982<code>[].copyWithin(0, 1, 2.999)<\/code>\u7b49\u540c\u4e8e <code>[].copyWithin(0, 1, 2)<\/code>\uff0c\u4f8b\u5982\uff1a\n<pre>\/\/ \u7ed3\u679c\u662f\uff1a[2, 2, 3, 4]\r\n[1, 2, 3, 4].copyWithin(0, 1, 2.9999)<\/pre>\n<\/li>\n<li>end\u5982\u679c\u662f\u8d1f\u6570\uff0c\u5219\u4ece\u6570\u7ec4\u7684\u540e\u9762\u5f00\u59cb\u53d6\u503c\uff0c\u7b49\u540c\u4e8e end + arr.length\uff1b<\/li>\n<li>end\u5982\u679c\u662f\u8d1f\u6570\uff0c\u540c\u65f6\u7edd\u5bf9\u503c\u5c0f\u4e8e -1 * arr.length\uff0c\u5219\u5f53\u4f5c 0 \u5904\u7406\u3002<\/li>\n<li>end\u5982\u679c\u8d85\u51fa\u6570\u7ec4\u7684\u957f\u5ea6\u8303\u56f4\uff0c\u5219\u5f53\u4f5c<code>array.length<\/code>\u5904\u7406\u3002<\/li>\n<li>\u5982\u679cend\u5bf9\u5e94\u7684\u4f4d\u7f6e\u6bd4start\u5c0f\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u590d\u5236\u3002<\/li>\n<\/ul>\n<h3>\u4e09\u3001copyWithin\u5b9e\u9645\u5e94\u7528<\/h3>\n<p>\u6211\u60f3\u7684\u4e00\u4e9b\u5e94\u7528\uff0c\u4e5f\u627e\u4e86\u4e00\u4e9b\u5e94\u7528\uff0c \u6b22\u8fce\u5927\u5bb6\u8865\u5145\u66f4\u9002\u5408\u4f7f\u7528<code>copyWithin()<\/code>\u7684\u573a\u666f\u3002<\/p>\n<h4>1. \u5220\u9664\u6570\u7ec4\u4e2d\u95f4\u67d0\u4e00\u9879<\/h4>\n<p>\u6570\u7ec4\u5220\u9664\u7b2c\u4e00\u4e2a\u9879\u76ee\u53ef\u4ee5\u4f7f\u7528 shift() \u65b9\u6cd5\uff0c\u5220\u9664\u6700\u540e\u4e00\u4e2a\u9879\u76ee\u53ef\u4ee5\u4f7f\u7528 pop() \u65b9\u6cd5\uff0c\u4f46\u4f3c\u4e4e\u6ca1\u6709\u5220\u9664\u4e2d\u95f4\u67d0\u4e00\u9879\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u8fc7\u53bb\u90fd\u662f\u4f7f\u7528 splice() \u65b9\u6cd5\u5b9e\u73b0\u7684\uff0c\u8bed\u6cd5\u793a\u610f\uff1a<\/p>\n<pre>[].splice(index, 1);<\/pre>\n<p>\u73b0\u5728\u591a\u4e86\u4e2a\u9009\u62e9\uff0c\u4f7f\u7528 <code>copyWithin()<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>[].copyWithin(index, index + 1).pop();<\/pre>\n<p>\u4f8b\u5982\uff0c\u5220\u9664\u6570\u7ec4 arr \u4e2d\u7684 \u7b2c 2 \u9879\uff1a<\/p>\n<pre>arr = ['\u7532', '\u4e59', '\u4e19', '\u4e01'];\r\narr.copyWithin(1, 2).pop();\r\n\/\/ \u7ed3\u679c\u662f['\u7532', '\u4e19', '\u4e01']\r\nconsole.log(arr);<\/pre>\n<h4>2. \u7f6e\u9876\u6570\u7ec4\u67d0\u4e00\u9879<\/h4>\n<p>\u8fd9\u79cd\u6570\u7ec4\u603b\u957f\u5ea6\u4e0d\u53d8\u7684\u573a\u666f\u6bd4\u8f83\u9002\u5408 copyWithin \u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ \u81ea\u5b9a\u4e49top\u7f6e\u9876\u65b9\u6cd5\r\nArray.prototype.top = function (index) {\r\n    const value = this[index];\r\n    this.copyWithin(1, 0, index);\r\n    this[0] = value;\r\n};<\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>arr = ['\u7532', '\u4e59', '\u4e19', '\u4e01'];\r\narr.top(1);\r\n\/\/ \u7ed3\u679c\u662f[\"\u4e59\", \"\u7532\", \"\u4e19\", \"\u4e01\"]\r\nconsole.log(arr);<\/pre>\n<h4>3. \u6a21\u62df\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5<\/h4>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u6848\u4f8b\u6e90\u81ea<a href=\"https:\/\/stackoverflow.com\/questions\/47947697\/for-which-use-cases-does-array-prototype-copywithin-exist\" rel=\"noopener\" target=\"_blank\">\u8fd9\u4e2astackoverflow<\/a>\uff1a<\/p>\n<pre>const insertionSort = (data, compare) => {\r\n  const arr = [...data];\r\n  let unsort_index = 1;\r\n\r\n  while (unsort_index < arr.length) {\r\n\r\n    while (arr[unsort_index] >= arr[unsort_index - 1]) unsort_index += 1;\r\n    const pick = arr[unsort_index];\r\n\r\n    let iter = 0;\r\n    while (iter < unsort_index &#038;&#038; arr[iter] < pick) iter += 1;\r\n    arr.copyWithin(iter + 1, iter, unsort_index);\r\n    arr[iter] = pick;\r\n\r\n    unsort_index += 1;\r\n  }\r\n  return arr;\r\n}\r\n\r\nconst input = [2, 3, 5, 1, 9, 8, 6, 6];\r\nconst asc = (a, b) => a - b;\r\nconst dsc = (a, b) => b - a;\r\n\r\nconsole.log({ input, asc_sorted: insertionSort(input, asc) });\r\nconsole.log({ input, dsc_sorted: insertionSort(input, dsc) });<\/pre>\n<p>\u7b49\u3002<\/p>\n<h3>\u56db\u3001\u7ed3\u675f\u8bed<\/h3>\n<p><code>copyWithin()<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u9ad8\u6027\u80fd\u7684\u6570\u7ec4\u79fb\u52a8\u65b9\u6cd5\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u6211\u67e5\u627e\u4e86\u4e00\u5708\uff0c\u6682\u65f6\u6ca1\u6709\u53d1\u73b0\u7279\u522b\u9002\u5408\u8fd9\u4e2a\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\uff0c\u7406\u8bba\u4e0a\uff0c\u8fd9\u4e2a\u4ec5\u9002\u5408\u5f53\u524d\u6570\u636e\u5185\u590d\u5236\u5e76\u7c98\u8d34\u7684\u573a\u666f\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4\u4e00\u5f20\u753b\u5e03\u5185\u590d\u5236\u7c98\u8d34\uff0c\u5bf9\u4e8e\u5728\u6570\u636e\u5c42\u9762\uff0c\u8fd9\u6837\u7684\u573a\u666f\u5e76\u4e0d\u591a\uff0c\u56e0\u4e3a\u76f4\u63a5\u8986\u76d6\u6570\u636e\uff0c\u8fd8\u662f\u7528\u65c1\u8fb9\u7684\u6570\u636e\u8986\u76d6\uff0c\u5e76\u4e0d\u5e38\u89c1\u3002<\/p>\n<p>\u4e5f\u53ef\u80fd\u662f\u81ea\u5df1\u76ee\u524d\u6240\u63a5\u89e6\u7684\u5f00\u53d1\u573a\u666f\u8fd8\u4e0d\u591f\u591a\u3002<\/p>\n<p>\u5e0c\u671b\u4ee5\u540e\u53ef\u4ee5\u78b0\u5230\u7279\u522b\u9002\u5408\u4f7f\u7528 <code>copyWithin()<\/code> \u65b9\u6cd5\u4f7f\u7528\u7684\u573a\u666f\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\u3002<\/p>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u5199\u5f97\u8fd8\u4e0d\u9519\uff0c\u6b22\u8fce\u8f6c\u53d1\uff01<\/p>\n<p>?<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10667\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10667<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202212\/copywithin-cover.png\" width=\"160\" height=\"102\" alt=\"copywithin\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>copyWithin()\u662f\u4e00\u4e2a\u6570\u7ec4\u5185\u7684\u590d\u5236\u4e0e\u66ff\u6362\u65b9\u6cd5\uff0c\u7c7b\u4f3cC\u6216C++\u91cc\u9762\u7684memmove\uff0c\u662f\u4e2a\u9ad8\u6027\u80fd\u7684\u6570\u636e\u79fb\u52a8\u65b9\u6cd5\uff0c\u5176\u8bed\u6cd5\u9700\u8981\u591a\u770b\u51e0\u7bc7\u624d\u770b\u5f97\u61c2\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1887,1884,904,1885,1886,669],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10667"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=10667"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10667\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}