{"id":2547,"date":"2024-11-10T09:47:24","date_gmt":"2024-11-10T01:47:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2547\/"},"modified":"2024-11-10T09:47:24","modified_gmt":"2024-11-10T01:47:24","slug":"%e6%95%b0%e7%bb%84%e6%89%93%e5%8d%b0%e6%97%b6%e4%ba%a4%e6%8d%a2%e5%89%8d%e5%90%8e%e4%b8%8d%e4%b8%80%e8%87%b4%ef%bc%9a%e4%b8%ba%e4%bd%95-json-parsejson-stringifyarray-%e5%88%9b%e5%bb%ba%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2547\/","title":{"rendered":"\u6570\u7ec4\u6253\u5370\u65f6\u4ea4\u6362\u524d\u540e\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4f55 `JSON.parse(JSON.stringify(array))` \u521b\u5efa\u7684\u526f\u672c\u5931\u6548\u4e86\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173079031637303.jpg\" class=\"aligncenter\" title=\"\u6570\u7ec4\u6253\u5370\u65f6\u4ea4\u6362\u524d\u540e\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4f55 `JSON.parse(JSON.stringify(array))` \u521b\u5efa\u7684\u526f\u672c\u5931\u6548\u4e86\uff1f\u63d2\u56fe\" alt=\"\u6570\u7ec4\u6253\u5370\u65f6\u4ea4\u6362\u524d\u540e\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4f55 `JSON.parse(JSON.stringify(array))` \u521b\u5efa\u7684\u526f\u672c\u5931\u6548\u4e86\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6570\u7ec4\u6253\u5370\u65f6\u7684\u5947\u602a\u73b0\u8c61\uff1a\u4ea4\u6362\u524d\u540e\u4e3a\u4f55\u4e0d\u540c\uff1f<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728\u7684\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u6253\u5370\u5143\u7d20\u4ea4\u6362\u524d\u540e\u6570\u7ec4\u7ed3\u6784\u65f6\uff0c\u53d1\u73b0\u4e24\u4e2a\u6253\u5370\u7ed3\u679c\u4e0d\u4e00\u81f4\u3002\u5143\u7d20\u4ea4\u6362\u524d\u6253\u5370\u7684\u6570\u7ec4\u7ed3\u6784\u4e0e\u5143\u7d20\u4ea4\u6362\u540e\u6253\u5370\u7684\u6570\u7ec4\u7ed3\u6784\u5b58\u5728\u5dee\u5f02\u3002<\/p>\n<p><strong>\u5206\u6790\uff1a<\/strong><\/p>\n<p>\u4ee3\u7801\u4e2d\u4f7f\u7528 oldArr = JSON.parse(JSON.stringify(array)) \u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u7ec4\u526f\u672c oldArr, \u7528\u4e8e\u4fdd\u5b58\u4ea4\u6362\u524d\u7684\u6570\u7ec4\u7ed3\u6784\u3002\u4f46\u95ee\u9898\u51fa\u73b0\u4e86\uff0c\u5728\u5143\u7d20\u4ea4\u6362\u540e\uff0c\u6ca1\u6709\u4f7f\u7528\u5206\u53f7\u5c06\u8fd4\u56de\u503c\u4e0e oldArr \u5206\u9694\u5f00\uff0c\u5bfc\u81f4 oldArr \u4fdd\u5b58\u7684\u5b9e\u9645\u4e0a\u662f\u5143\u7d20\u4ea4\u6362\u540e\u7684\u6570\u7ec4\u7ed3\u6784\u3002<\/p>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u5e94\u4e3a\uff1a<\/p>\n<pre>const oldArr = JSON.parse(JSON.stringify(array));\n[array[i], array[j]] = [array[j], array[i]];\nconsole.log(oldArr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd oldArr \u4fdd\u5b58\u5728\u5143\u7d20\u4ea4\u6362\u524d\u7684\u6570\u7ec4\u7ed3\u6784\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>function bubbleSort(array) {\n            const len = array.length\n            if (len &lt; 2) return array\n            for (let i = 0; i &lt; len - 1; i++) {\n                for (let j = i + 1; j &lt; len; j++) {\n                    if (array[j] &lt; array[i]) {\n                        const oldArr = JSON.parse(JSON.stringify(array));\n                        [array[i], array[j]] = [array[j], array[i]];\n                        console.log(oldArr);\n                    }\n                }\n            }\n            return array\n        }\n\n        var arr = [1, 3, 6, 3, 23, 1, 34];\n        bubbleSort(arr)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>[1, 3]\n[3, 6]\n[1, 6]\n[1, 3]\n[1, 23]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5143\u7d20\u4ea4\u6362\u524d\u540e\u7684\u6253\u5370\u7ed3\u679c\u4e00\u81f4\uff0c\u89e3\u51b3\u4e86\u539f\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4\u6253\u5370\u65f6\u4ea4\u6362\u524d\u540e\u4e0d\u4e00\u81f4\uff1a\u4e3a\u4f55 `JSON.parse(JSON.stringify(array))` \u521b\u5efa\u7684\u526f\u672c\u5931\u6548\u4e86\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>\u6570\u7ec4\u6253\u5370\u65f6\u7684\u5947\u602a\u73b0\u8c61\uff1a\u4ea4\u6362\u524d\u540e\u4e3a\u4f55\u4e0d\u540c\uff1f \u95ee\u9898\u63cf\u8ff0\uff1a \u5728\u7684\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u6253\u5370\u5143\u7d20\u4ea4\u6362\u524d\u540e\u6570\u7ec4\u7ed3\u6784\u65f6\uff0c\u53d1\u73b0\u4e24\u4e2a\u6253\u5370\u7ed3\u679c\u4e0d\u4e00\u81f4\u3002\u5143\u7d20\u4ea4\u6362\u524d\u6253\u5370\u7684\u6570\u7ec4\u7ed3\u6784\u4e0e\u5143\u7d20\u4ea4\u6362\u540e\u6253\u5370\u7684\u6570\u7ec4\u7ed3\u6784\u5b58\u5728\u5dee\u5f02\u3002 \u5206\u6790\uff1a \u4ee3\u7801\u4e2d\u4f7f\u7528 oldArr = JSON.parse(JSON.stringify(array)) \u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u7ec4\u526f\u672c oldArr, \u7528\u4e8e\u4fdd\u5b58\u4ea4\u6362\u524d\u7684\u6570\u7ec4\u7ed3\u6784\u3002\u4f46\u95ee\u9898\u51fa\u73b0\u4e86\uff0c\u5728\u5143\u7d20\u4ea4\u6362\u540e\uff0c\u6ca1\u6709\u4f7f\u7528\u5206\u53f7\u5c06\u8fd4\u56de\u503c\u4e0e oldArr \u5206\u9694\u5f00\uff0c\u5bfc\u81f4 oldArr \u4fdd\u5b58\u7684\u5b9e\u9645\u4e0a\u662f\u5143\u7d20\u4ea4\u6362\u540e\u7684\u6570\u7ec4\u7ed3\u6784\u3002 \u6b63\u786e\u7684\u65b9\u6cd5\u5e94\u4e3a\uff1a const oldArr = JSON.parse(JSON.stringify(array)); [array[i], array[j]] = [array[j], array[i]]; console.log(oldArr); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd oldArr \u4fdd\u5b58\u5728\u5143\u7d20\u4ea4\u6362\u524d\u7684\u6570\u7ec4\u7ed3\u6784\u3002 \u793a\u4f8b\uff1a \u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a function bubbleSort(array) { const len = array.length if (len &lt; 2) return array for (let i = 0; i &lt; len &#8211; 1; [&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-2547","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2547","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=2547"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2547\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}