{"id":360,"date":"2022-05-24T14:01:23","date_gmt":"2022-05-24T06:01:23","guid":{"rendered":"https:\/\/www.keioi.cn\/?p=360"},"modified":"2022-05-24T14:02:57","modified_gmt":"2022-05-24T06:02:57","slug":"js%e6%89%93%e4%b9%b1%e6%95%b0%e7%bb%84%ef%bc%8c%e6%95%b0%e7%bb%84%e6%96%b9%e6%b3%95%e3%80%82","status":"publish","type":"post","link":"https:\/\/www.keioi.cn\/archives\/360","title":{"rendered":"js\u6253\u4e71\u6570\u7ec4\uff0c\u6570\u7ec4\u65b9\u6cd5\u3002"},"content":{"rendered":"\n<p>function randomsort(a,b){<\/p>\n\n\n\n<p>return Math.random()&gt;0.5 ? -1 : 1;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>var arr=[1,2,3,4,5,6,7,8];<\/p>\n\n\n\n<p>arr.sort(randomsort);<\/p>\n\n\n\n<p><strong>sort() \u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n\n\n\n<p>\u4f20\u6bd4\u8f83\u51fd\u6570\u7684\u8bdd\uff0c\u9ed8\u8ba4\u6309\u5b57\u6bcd\u5347\u5e8f\uff0c\u5982\u679c\u4e0d\u662f\u5143\u7d20\u4e0d\u662f\u5b57\u7b26\u4e32\u7684\u8bdd\uff0c\u4f1a\u8c03\u7528toString()<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-medium-font-size\"><strong>\u521b\u5efa\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p><strong>ES6 Array.of() \u8fd4\u56de\u7531\u6240\u6709\u53c2\u6570\u503c\u7ec4\u6210\u7684\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p>Array.of(3, 11, 8); \/\/ [3,11,8]<\/p>\n\n\n\n<p><strong>ES6 Arrar.from() \u5c06\u4e24\u7c7b\u5bf9\u8c61\u8f6c\u4e3a\u771f\u6b63\u7684\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570(\u5fc5\u9700):\u8981\u8f6c\u5316\u4e3a\u771f\u6b63\u6570\u7ec4\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570(\u53ef\u9009): \u7c7b\u4f3c\u6570\u7ec4\u7684map\u65b9\u6cd5\uff0c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u5c06\u5904\u7406\u540e\u7684\u503c\u653e\u5165\u8fd4\u56de\u7684\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570(\u53ef\u9009): \u7528\u6765\u7ed1\u5b9athis\u3002<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-medium-font-size\"><strong>\u6539\u53d8\u539f\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p><strong>pop() \u5220\u9664\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u7684\u4e00\u4e2a\u5143\u7d20<\/strong>\uff0c\u5e76\u4e14\u8fd4\u56de\u8fd9\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p><strong>shift() \u5220\u9664\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong>\uff0c\u5e76\u4e14\u8fd4\u56de\u8fd9\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p><strong>push() \u5411\u6570\u7ec4\u7684\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20<\/strong>\uff0c\u5e76\u8fd4\u56de\u65b0\u7684\u957f\u5ea6<\/p>\n\n\n\n<p><strong>unshift()<\/strong> <strong>\u65b9\u6cd5\u53ef\u5411\u6570\u7ec4\u7684\u5f00\u5934\u6dfb\u52a0\u4e00\u4e2a\u6216\u66f4\u591a\u5143\u7d20<\/strong>\uff0c\u5e76\u8fd4\u56de\u65b0\u7684\u957f\u5ea6<\/p>\n\n\n\n<p><strong>reverse() \u98a0\u5012\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u987a\u5e8f<\/strong><\/p>\n\n\n\n<p><strong>splice() \u6dfb\u52a0\/\u5220\u9664\u6570\u7ec4\u5143\u7d20<\/strong>\uff0c\u7136\u540e\u8fd4\u56de\u88ab\u5220\u9664\u7684\u9879\u76ee<\/p>\n\n\n\n<p>index\uff1a\u5fc5\u9700\u3002\u6574\u6570\uff0c\u89c4\u5b9a\u6dfb\u52a0\/\u5220\u9664\u9879\u76ee\u7684\u4f4d\u7f6e\uff0c\u4f7f\u7528\u8d1f\u6570\u53ef\u4ece\u6570\u7ec4\u7ed3\u5c3e\u5904\u89c4\u5b9a\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>howmany\uff1a\u5fc5\u9700\u3002\u8981\u5220\u9664\u7684\u9879\u76ee\u6570\u91cf\u3002\u5982\u679c\u8bbe\u7f6e\u4e3a 0\uff0c\u5219\u4e0d\u4f1a\u5220\u9664\u9879\u76ee<\/p>\n\n\n\n<p>item1, \u2026, itemX\uff1a \u53ef\u9009\u3002\u5411\u6570\u7ec4\u6dfb\u52a0\u7684\u65b0\u9879\u76ee\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u503c: \u5982\u679c\u6709\u5143\u7d20\u88ab\u5220\u9664,\u8fd4\u56de\u5305\u542b\u88ab\u5220\u9664\u9879\u76ee\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n\n\n\n<p><strong>sort() \u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n\n\n\n<p>\u65b9\u6cd5\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e2a\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>sort\u7684\u6bd4\u8f83\u51fd\u6570\u6709\u4e24\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u8981\u5728\u51fd\u6570\u4e2d\u63a5\u6536\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e24\u4e2a\u53c2\u6570\u662f\u6570\u7ec4\u4e2d\u4e24\u4e2a\u8981\u6bd4\u8f83\u7684\u5143\u7d20\uff0c\u901a\u5e38\u6211\u4eec\u7528 a \u548c b \u63a5\u6536\u4e24\u4e2a\u5c06\u8981\u6bd4\u8f83\u7684\u5143\u7d20\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u82e5\u6bd4\u8f83\u51fd\u6570\u8fd4\u56de\u503c&lt;0\uff0c\u90a3\u4e48a\u5c06\u6392\u5230b\u7684\u524d\u9762;<\/li><li>\u82e5\u6bd4\u8f83\u51fd\u6570\u8fd4\u56de\u503c=0\uff0c\u90a3\u4e48a \u548c b \u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u53d8\uff1b<\/li><li>\u82e5\u6bd4\u8f83\u51fd\u6570\u8fd4\u56de\u503c&gt;0\uff0c\u90a3\u4e48b \u6392\u5728a \u5c06\u7684\u524d\u9762\uff1b<\/li><\/ul>\n\n\n\n<p><strong>ES6: copyWithin() \u6307\u5b9a\u4f4d\u7f6e\u7684\u6210\u5458\u590d\u5236\u5230\u5176\u4ed6\u4f4d\u7f6e<\/strong><\/p>\n\n\n\n<p>\u5728\u5f53\u524d\u6570\u7ec4\u5185\u90e8\uff0c\u5c06\u6307\u5b9a\u4f4d\u7f6e\u7684\u6210\u5458\u590d\u5236\u5230\u5176\u4ed6\u4f4d\u7f6e,\u5e76\u8fd4\u56de\u8fd9\u4e2a\u6570\u7ec4\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>target\uff08\u5fc5\u9700\uff09\uff1a\u4ece\u8be5\u4f4d\u7f6e\u5f00\u59cb\u66ff\u6362\u6570\u636e\u3002\u5982\u679c\u4e3a\u8d1f\u503c\uff0c\u8868\u793a\u5012\u6570\u3002<\/li><li>start\uff08\u53ef\u9009\uff09\uff1a\u4ece\u8be5\u4f4d\u7f6e\u5f00\u59cb\u8bfb\u53d6\u6570\u636e\uff0c\u9ed8\u8ba4\u4e3a 0\u3002\u5982\u679c\u4e3a\u8d1f\u503c\uff0c\u8868\u793a\u5012\u6570\u3002<\/li><li>end\uff08\u53ef\u9009\uff09\uff1a\u5230\u8be5\u4f4d\u7f6e\u524d\u505c\u6b62\u8bfb\u53d6\u6570\u636e\uff0c\u9ed8\u8ba4\u7b49\u4e8e\u6570\u7ec4\u957f\u5ea6\u3002\u4f7f\u7528\u8d1f\u6570\u53ef\u4ece\u6570\u7ec4\u7ed3\u5c3e\u5904\u89c4\u5b9a\u4f4d\u7f6e\u3002<\/li><li><strong>fill() \u586b\u5145\u6570\u7ec4<\/strong><\/li><\/ol>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u5143\u7d20(\u5fc5\u987b): \u8981\u586b\u5145\u6570\u7ec4\u7684\u503c<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a\u5143\u7d20(\u53ef\u9009): \u586b\u5145\u7684\u5f00\u59cb\u4f4d\u7f6e,\u9ed8\u8ba4\u503c\u4e3a0<\/p>\n\n\n\n<p>\u7b2c\u4e09\u4e2a\u5143\u7d20(\u53ef\u9009)\uff1a\u586b\u5145\u7684\u7ed3\u675f\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u662f\u4e3athis.length<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-medium-font-size\"><strong>\u4e0d\u6539\u53d8\u539f\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p><strong>join() \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32<\/strong><\/p>\n\n\n\n<p>str(\u53ef\u9009): \u6307\u5b9a\u8981\u4f7f\u7528\u7684\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n\n\n\n<p><strong>toLocaleString() \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32<\/strong><\/p>\n\n\n\n<p><strong>toString() \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32 \u4e0d\u63a8\u8350<\/strong><\/p>\n\n\n\n<p><strong>slice() \u6d45\u62f7\u8d1d\u6570\u7ec4\u7684\u5143\u7d20<\/strong><\/p>\n\n\n\n<p>begin(\u53ef\u9009): \u7d22\u5f15\u6570\u503c,\u63a5\u53d7\u8d1f\u503c\uff0c\u4ece\u8be5\u7d22\u5f15\u5904\u5f00\u59cb\u63d0\u53d6\u539f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20,\u9ed8\u8ba4\u503c\u4e3a0\u3002<\/p>\n\n\n\n<p>end(\u53ef\u9009):\u7d22\u5f15\u6570\u503c(\u4e0d\u5305\u62ec),\u63a5\u53d7\u8d1f\u503c\uff0c\u5728\u8be5\u7d22\u5f15\u5904\u524d\u7ed3\u675f\u63d0\u53d6\u539f\u6570\u7ec4\u5143\u7d20\uff0c\u9ed8\u8ba4\u503c\u4e3a\u6570\u7ec4\u672b\u5c3e(\u5305\u62ec\u6700\u540e\u4e00\u4e2a\u5143\u7d20)\u3002<\/p>\n\n\n\n<p><strong>cancat\uff08\uff09\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u6570\u7ec4\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p>arrayX\uff08\u5fc5\u987b\uff09\uff1a\u8be5\u53c2\u6570\u53ef\u4ee5\u662f\u5177\u4f53\u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u662f\u6570\u7ec4\u5bf9\u8c61\u3002\u53ef\u4ee5\u662f\u4efb\u610f\u591a\u4e2a<\/p>\n\n\n\n<p><strong>indexOf() \u67e5\u627e\u6570\u7ec4\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5143\u7d20\uff0c\u8fd4\u56de\u4e0b\u6807<\/strong><\/p>\n\n\n\n<p>searchElement(\u5fc5\u987b):\u88ab\u67e5\u627e\u7684\u5143\u7d20<\/p>\n\n\n\n<p>fromIndex(\u53ef\u9009):\u5f00\u59cb\u67e5\u627e\u7684\u4f4d\u7f6e(\u4e0d\u80fd\u5927\u4e8e\u7b49\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u8fd4\u56de-1)\uff0c\u63a5\u53d7\u8d1f\u503c\uff0c\u9ed8\u8ba4\u503c\u4e3a0\u3002<\/p>\n\n\n\n<p><strong>lastIndexOf() \u67e5\u627e\u6307\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e<\/strong><\/p>\n\n\n\n<p>searchElement(\u5fc5\u987b): \u88ab\u67e5\u627e\u7684\u5143\u7d20<\/p>\n\n\n\n<p>fromIndex(\u53ef\u9009): \u9006\u5411\u67e5\u627e\u5f00\u59cb\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u503c\u6570\u7ec4\u7684\u957f\u5ea6-1\uff0c\u5373\u67e5\u627e\u6574\u4e2a\u6570\u7ec4\u3002<\/p>\n\n\n\n<p><strong>ES7 includes() \u67e5\u627e\u6570\u7ec4\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20 \u8fd4\u56de\u5e03\u5c14<\/strong><\/p>\n\n\n\n<p>searchElement(\u5fc5\u987b):\u88ab\u67e5\u627e\u7684\u5143\u7d20<\/p>\n\n\n\n<p>fromIndex(\u53ef\u9009):\u9ed8\u8ba4\u503c\u4e3a0\uff0c\u53c2\u6570\u8868\u793a\u641c\u7d22\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u63a5\u53d7\u8d1f\u503c\u3002\u6b63\u503c\u8d85\u8fc7\u6570\u7ec4\u957f\u5ea6\uff0c\u6570\u7ec4\u4e0d\u4f1a\u88ab\u641c\u7d22\uff0c\u8fd4\u56defalse\u3002\u8d1f\u503c\u7edd\u5bf9\u503c\u8d85\u8fc7\u957f\u6570\u7ec4\u5ea6\uff0c\u91cd\u7f6e\u4ece0\u5f00\u59cb\u641c\u7d22\u3002<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-medium-font-size\"><strong>\u904d\u5386\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p><strong>forEach<\/strong><\/p>\n\n\n\n<p>array.forEach(function(currentValue, index, arr), thisValue)<\/p>\n\n\n\n<p>1. currentValue(\u5fc5\u987b),\u6570\u7ec4\u5f53\u524d\u5143\u7d20\u7684\u503c<br>2. index(\u53ef\u9009), \u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u503c<br>3. arr(\u53ef\u9009),\u6570\u7ec4\u5bf9\u8c61\u672c\u8eab<\/p>\n\n\n\n<p><strong>every \u68c0\u6d4b\u6570\u7ec4\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u7b26\u5408\u5224\u65ad\u6761\u4ef6<\/strong><\/p>\n\n\n\n<p>array.every(function(currentValue, index, arr), thisValue)\u6570\u7ec4\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u7b26\u5408\u51fd\u6570\u5b9a\u4e49\u7684\u6761\u4ef6<\/p>\n\n\n\n<p><strong>some \u6570\u7ec4\u4e2d\u7684\u662f\u5426\u6709\u6ee1\u8db3\u5224\u65ad\u6761\u4ef6\u7684\u5143\u7d20<\/strong><\/p>\n\n\n\n<p>array.some(function(currentValue, index, arr), thisValue)\u6570\u7ec4\u4e2d\u7684\u662f\u5426\u6709\u6ee1\u8db3\u5224\u65ad\u6761\u4ef6\u7684\u5143\u7d20<\/p>\n\n\n\n<p><strong>filter \u8fc7\u6ee4\u539f\u59cb\u6570\u7ec4\uff0c\u8fd4\u56de\u65b0\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p>let new_array = arr.filter(function(currentValue, index, arr), thisArg)<\/p>\n\n\n\n<p><strong>map \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u8fd4\u56de\u65b0\u7684\u6570\u7ec4<\/strong><\/p>\n\n\n\n<p>let new_array = arr.map(function(currentValue, index, arr), thisArg)<\/p>\n\n\n\n<p><strong>reduce \u4e3a\u6570\u7ec4\u63d0\u4f9b\u7d2f\u52a0\u5668\uff0c\u5408\u5e76\u4e3a\u4e00\u4e2a\u503c<\/strong><\/p>\n\n\n\n<p><strong>reduceRight \u4ece\u53f3\u81f3\u5de6\u7d2f\u52a0<\/strong> \u4e0ereduce\u6267\u884c\u65b9\u5411\u76f8\u53cd<\/p>\n\n\n\n<p><strong>ES6\uff1afind()&amp; findIndex() \u6839\u636e\u6761\u4ef6\u627e\u5230\u6570\u7ec4\u6210\u5458<\/strong><\/p>\n\n\n\n<p>let new_array = arr.find(function(currentValue, index, arr), thisArg)<\/p>\n\n\n\n<p>let a = [1, 4, -5, 10].find((n) =&gt; n &lt; 0); \/\/ \u8fd4\u56de\u5143\u7d20-5<\/p>\n\n\n\n<p>let a = [1, 4, -5, 10].findIndex((n) =&gt; n &lt; 0); \/\/ \u8fd4\u56de\u7d22\u5f152<\/p>\n\n\n\n<p><strong>ES6 keys()&amp;values()&amp;entries() \u904d\u5386\u952e\u540d\u3001\u904d\u5386\u952e\u503c\u3001\u904d\u5386\u952e\u540d+\u952e\u503c<\/strong><\/p>\n\n\n\n<p>for(let index of [&#8216;a&#8217;, &#8216;b&#8217;].keys()) {&nbsp;console.log(index);}\/\/ 0\/\/ 1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>function randoms&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,4],"tags":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-js","category-4"],"_links":{"self":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/comments?post=360"}],"version-history":[{"count":2,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/posts\/360\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keioi.cn\/api\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}