{"id":11519,"date":"2025-02-06T21:42:25","date_gmt":"2025-02-06T13:42:25","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11519"},"modified":"2025-02-06T21:42:25","modified_gmt":"2025-02-06T13:42:25","slug":"js-array-with","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/02\/js-array-with\/","title":{"rendered":"JS Array\u6570\u7ec4\u65b0\u7684with\u65b9\u6cd5\uff0c\u4f60\u77e5\u9053\u4f5c\u7528\u5417\uff1f"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11519\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11519<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202502-array-with.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202502\/js-with-cover.jpg\" width=\"480\" height=\"303\" alt=\"\u6570\u7ec4Array with\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u4e0d\u6539\u53d8\u539f\u6570\u7ec4\u8fdb\u884c\u8bbe\u7f6e<\/h3>\n<p>\u5148\u770b\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<p>\u5728\u8fc7\u53bb\uff0c\u6211\u4eec\u4fee\u6539\u67d0\u4e2a\u6570\u7ec4\u9879\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u7d22\u5f15\u503c\u8fdb\u884c\u8bbe\u7f6e\uff0c\u4f8b\u5982\u5c06\u6570\u7ec4\u7684\u7b2c2\u9879\u4fee\u6539\u4e3a\u67d0\u67d0\u7535\u5f71\u540d\u79f0\uff1a<\/p>\n<pre>const arr = ['\u9b54\u7ae5\u95f9\u6d77', '\u86df\u9f99\u884c\u52a8', '\u4fa0\u4e4b\u5927\u8005', '\u5510\u63a21900'];\r\n\/\/ \u4fee\u6539\u6570\u7ec4\u7b2c2\u9879\r\narr[1] = '\u6d41\u6d6a\u5730\u7403';<\/pre>\n<p>\u4f46\u8fd9\u79cd\u4fee\u6539\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u539f\u6570\u7ec4\u4e0d\u53d1\u751f\u6539\u53d8\uff0c\u5728\u8fc7\u53bb\uff0c\u90fd\u662f\u5148\u8981\u590d\u5236\u4e00\u4e0b\u6570\u7ec4\uff0c\u7136\u540e\u518d\u5904\u7406\u3002<\/p>\n<p>\u590d\u5236\u6570\u7ec4\u7684\u65b9\u6cd5\u975e\u5e38\u591a\uff0c\u4e0b\u9762\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\uff1a<\/p>\n<pre>\r\n\/\/ \u539f\u6570\u7ec4\r\nconst fruits = [\"Apple\", \"Mango\"];\r\n\r\n\/\/ \u590d\u5236\u6570\u7ec4\uff0c\u4f7f\u7528...\u6269\u5c55\u8bed\u6cd5\r\nconst fruitsCopy = [... fruits]\r\n\r\n\/\/ \u590d\u5236\u6570\u7ec4\uff0c\u4f7f\u7528from()\u9759\u6001\u65b9\u6cd5\r\nconst fruitsCopy2 = Array.form(fruits)\r\n\r\n\/\/ \u590d\u5236\u6570\u7ec4\uff0c\u4f7f\u7528slice()\u65b9\u6cd5\r\nconst fruitsCopy3 = fruits.slice()\r\n\r\n\/\/ \u590d\u5236\u6570\u7ec4\uff0c\u4f7f\u7528concat()\u65b9\u6cd5\r\nconst fruitsCopy4 = fruits.concat()<\/pre>\n<p>\u6211\u4e2a\u4eba\u6bd4\u8f83\u559c\u6b22\u4f7f\u7528<code>slice()<\/code>\u65b9\u6cd5\uff0c\u8bed\u4e49\u597d\u7684\u540c\u65f6\u517c\u5bb9\u6027\u4e5f\u597d\uff0c\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>const arr = ['\u9b54\u7ae5\u95f9\u6d77', '\u86df\u9f99\u884c\u52a8', '\u4fa0\u4e4b\u5927\u8005', '\u5510\u63a21900'];\r\n\/\/ \u4fee\u6539\u6570\u7ec4\u7b2c2\u9879\r\nconst arr2 = arr.slice();\r\narr2[1] = '\u6d41\u6d6a\u5730\u7403';<\/pre>\n<h4>\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528with()\u65b9\u6cd5<\/h4>\n<p>\u800c\u5982\u4eca\uff0c\u65e0\u9700\u50cf\u8fc7\u53bb\u8fd9\u822c\u9ebb\u70e6\uff0c\u590d\u5236\u6570\u7ec4\u540c\u65f6\u8bbe\u7f6e\u6570\u7ec4\u9879\u7684\u503c\u6709\u4e86\u4e13\u95e8\u7684API\u65b9\u6cd5\uff0c\u4e3aArray.prototype.with()\u65b9\u6cd5\uff0c\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>const arr = [, '\u5510\u63a21900', '\u4fa0\u4e4b\u5927\u8005', '\u86df\u9f99\u884c\u52a8'];\r\n\/\/ \u7ed3\u679c\u662f ['\u54ea\u5412\u9b54\u7ae5\u95f9\u6d77', '\u5510\u63a21900', '\u4fa0\u4e4b\u5927\u8005', '\u86df\u9f99\u884c\u52a8']\r\nconsole.log(arr.with(0, '\u54ea\u5412\u9b54\u7ae5\u95f9\u6d77'));<\/pre>\n<p>\u5728Chrome\u63a7\u5236\u53f0\u4e2d\u7684\u8fd0\u884c\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202502\/2025-2-6_113236.png\" width=\"401\" height=\"91\" alt=\"with\u8fd0\u884c\u7ed3\u679c\u622a\u56fe\u793a\u610f\" class=\"alignnone size-medium\" style=\"border:1px solid #ddd\" \/><\/p>\n<h3>\u4e8c\u3001with\u7684\u8bed\u6cd5\u548c\u517c\u5bb9\u6027<\/h3>\n<p>\u4e0b\u9762\u7b80\u5355\u4ecb\u7ecd\u4e0b<code>with()<\/code>\u65b9\u6cd5\u7684\u8bed\u6cd5\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>arrayInstance.with(index, value)<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>index<\/code>\u662f\u7d22\u5f15\u503c\uff0c\u53ef\u4ee5\u662f\u8d1f\u6570\uff08\u6700\u7ec8\u7684\u7d22\u5f15\u503c=\u8d1f\u6570+\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u8be5\u53c2\u6570\u7684\u542b\u4e49\u548c\u6570\u7ec4\u7684at()\u65b9\u6cd5\u4e00\u6837\u3002\u5982\u679c\u7d22\u5f15\u503c\u8d85\u51fa\u8303\u56f4\uff0c\u5219\u4f1a\u62a5\u9519\uff0c\u8fd9\u4e2a\u8981\u6ce8\u610f\u4e0b\u3002<\/li>\n<li><code>value<\/code>\u6ca1\u4ec0\u4e48\u597d\u8bf4\u7684\uff0c\u5c31\u662f\u4fee\u6539\u7684\u503c\u3002\u5982\u679c\u8be5\u53c2\u6570\u4e0d\u8bbe\u7f6e\uff0c\u5219\u5bf9\u5e94\u7684\u6570\u7ec4\u9879\u4f1a\u53d8\u6210 <code>undefined<\/code>\u3002<\/li>\n<\/ul>\n<p>\u57fa\u672c\u4e0a\uff0c\u5728\u76ee\u524d\u7684Web\u5f00\u53d1\u5efa\u8bae\u4e2d\uff0c\u6570\u7ec4\u8bfb\u53d6\u4f7f\u7528at()\u65b9\u6cd5\uff0c\u5199\u503c\u4f7f\u7528with()\uff0c\u76f8\u5f97\u76ca\u5f70\u3002<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>2023\u5e74\u5de6\u53f3\u652f\u6301\u7684\uff0c\u76ee\u524d\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u652f\u6301\uff0c\u5177\u4f53\u53c2\u89c1\u4e0b\u9762\u7684caniuse\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202502\/2025-2-6_154007.png\" width=\"643\" height=\"320\" alt=\"with\u65b9\u6cd5\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4e0d\u8fc7\u5728\u6b63\u5f0f\u5bf9\u5916\u7684\u9879\u76ee\u4e2d\u6700\u597d\u5f15\u5165Polyfill\u4ee3\u7801\uff0c\u907f\u514d\u5f02\u5e38\u3002<\/p>\n<p>\u7c7b\u4f3c\u7684Polyfill\u9879\u76ee\u5f88\u591a\uff0c\u8fd9\u91cc\u662f\u6211\u627e\u7684<a href=\"https:\/\/github.com\/tc39\/proposal-change-array-by-copy\/blob\/main\/polyfill.js#L258\" rel=\"noopener\" target=\"_blank\">\u5b9e\u73b0\u6e90\u7801\u53c2\u8003<\/a>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202502\/2025-02-06_212019.png\" width=\"703\" height=\"400\" alt=\"polyfill\u6e90\u7801\u53c2\u8003\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001\u5176\u4ed6\u4fdd\u7559\u539f\u6570\u7ec4\u7684\u65b9\u6cd5<\/h3>\n<p>\u4e0e<code>with()<\/code>\u4e00\u540c\u652f\u6301\u7684\u8fd8\u6709\u4e0d\u5c11\u5176\u4ed6\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u4fdd\u7559\u539f\u6570\u7ec4\u7684\u540c\u65f6\u5b9e\u73b0\u6570\u7ec4\u7684\u76f8\u5173\u5904\u7406\uff0c\u5305\u62ec\u53cd\u5e8f\uff0c\u6392\u5e8f\u548c\u5207\u5272\u7b49\u3002<\/p>\n<p>\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>toReversed()<\/code>\uff0c\u4e0d\u6539\u53d8\u539f\u6570\u7ec4\uff0c\u540c\u65f6\u6570\u7ec4\u53cd\u5e8f\uff0c\u8fd4\u56de\u503c\u662f\u65b0\u6570\u7ec4\u3002\u6539\u53d8\u539f\u6570\u7ec4\u7684\u53cd\u5e8f\u65b9\u6cd5\u662f<code>reverse()<\/code>\u65b9\u6cd5\uff0c\u53c2\u6570\u90fd\u662f\u4e00\u6837\u7684\u3002<\/li>\n<li><code>toSorted()<\/code>\uff0c\u4e0d\u6539\u53d8\u539f\u59cb\u6570\u7ec4\uff0c\u540c\u65f6\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd4\u56de\u5185\u5bb9\u662f\u65b0\u6570\u7ec4\u3002\u5bf9\u5e94\u7684\u539f\u59cb\u65b9\u6cd5\u662f<code>sort()<\/code>\u65b9\u6cd5\uff0c\u4e24\u8005\u53c2\u6570\u4e00\u81f4\u3002<\/li>\n<li><code>toSpliced()<\/code>\uff0c\u4e0d\u6539\u53d8\u539f\u6765\u7684\u6570\u7ec4\uff0c\u540c\u65f6\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u5207\u5272\u548c\u63d2\u5165\u64cd\u4f5c\uff0c\u8fd4\u56de\u5904\u7406\u540e\u7684\u65b0\u6570\u7ec4\u3002\u8001\u65b9\u6cd5\u662f<code>splice()<\/code>\u65b9\u6cd5\uff0c\u53c2\u6570\u96f7\u540c\u3002<\/li>\n<\/ul>\n<p>\u5728React\u548cVue\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u6709\u65f6\u5019\u9700\u8981\u5bf9\u6570\u7ec4\u8fdb\u884c\u5904\u7406\u7684\u65f6\u5019\uff0c\u4e0d\u6539\u53d8\u539f\u6570\u7ec4\uff08\u4e0d\u4f1a\u81ea\u52a8\u89e6\u53d1\u91cd\u7ed8\uff09\uff0c\u4e0a\u9762\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u8003\u8651\u7684\u3002<\/p>\n<p>\u6d4f\u89c8\u5668\u540c\u4e00\u65f6\u671f\u652f\u6301\u7684\uff0c\u56e0\u6b64\uff0c\u517c\u5bb9\u6027\u662f\u4e00\u6837\u7684\u3002<\/p>\n<h3>\u56db\u3001\u65b0\u5e74\u4f0a\u59cb\uff0c\u5f00\u5de5\u5927\u5409<\/h3>\n<p>\u597d\u5427\uff0c\u672c\u6587\u5c31\u8fd9\u4e48\u70b9\u5185\u5bb9\u3002<\/p>\n<p>\u6625\u8282\u7ed3\u675f\uff0c\u5f00\u5de5\u5927\u5409\uff0c\u65b0\u7684\u4e00\u5e74\u7b97\u662f\u5f00\u59cb\u4e86\u3002<\/p>\n<p>\u7ee7\u7eed\u4fdd\u6301\u5bf9\u524d\u7aef\u7684\u597d\u5947\u5fc3\uff0c\u4e0d\u65ad\u5b66\u4e60\uff0c\u9759\u5f85\u5f00\u82b1\u7ed3\u679c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202502\/2025-2-6_211734.jpeg\" width=\"150\" height=\"147\" alt=\"\u65b0\u5e74\u5f00\u5de5\u5927\u5409\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11519\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11519<\/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\/202502\/js-with-cover.jpg\" width=\"160\" height=\"101\" alt=\"\u6570\u7ec4Array with\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>Web\u6d4f\u89c8\u5668\u53c8\u65b0\u589e\u591a\u4e2aArray\u6570\u7ec4API\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5728\u6570\u7ec4\u5904\u7406\u7684\u540c\u65f6\u5747\u4e0d\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\u5185\u5bb9\uff0c\u8fd9\u5728Vue\u3001React\u7b49\u5f00\u53d1\u573a\u666f\u4e2d\u9887\u4e3a\u53d7\u7528\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":[215,1887,2036,2037,1885,1886,2033,2034,2035,2038],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11519"}],"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=11519"}],"version-history":[{"count":7,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11519\/revisions"}],"predecessor-version":[{"id":11584,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11519\/revisions\/11584"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}