{"id":607,"date":"2023-12-30T09:01:01","date_gmt":"2023-12-30T01:01:01","guid":{"rendered":""},"modified":"2023-12-30T09:01:01","modified_gmt":"2023-12-30T01:01:01","slug":"\u6570\u7ec4\u53bb\u91cd\u7684\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/607.html","title":{"rendered":"\u6570\u7ec4\u53bb\u91cd\u7684\u65b9\u6cd5"},"content":{"rendered":"

\u5df2\u77e5\u6570\u7ec4arr=[3, 3, 5, 5, 7, 1, 8, 8]\uff0c\u53bb\u6389\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u6570\uff0c\u5f97\u5230newArr=[3, 5, 7, 1, 8]<\/p>\n

\u65b9\u6cd5\u4e00\uff1aindexOf<\/p>\n

var arr = [3, 3, 5, 5, 7, 1, 8, 8];\nfunction removeRepeat(){ \n       var newArr = [];    \/\/\u5b9a\u4e49\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 \n       for(var i = 0; i < arr.length; i++){    \/\/\u5faa\u73af\u904d\u5386\u5f53\u524d\u6570\u7ec4 \n           \/\/\u5224\u65ad\u5f53\u524d\u6570\u7ec4\u4e0b\u6807\u4e3ai\u7684\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u4fdd\u5b58\u5230\u4e34\u65f6\u6570\u7ec4 \n           \/\/\u5982\u679c\u5df2\u4fdd\u5b58\uff0c\u5219\u8df3\u8fc7\uff0c\u5426\u5219\u5c06\u6b64\u5143\u7d20\u4fdd\u5b58\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d \n           if(newArr.indexOf(arr[i]) === -1){ \n               newArr.push(arr[i]); \n           } \n       } \n       return newArr; \n   }<\/code><\/pre>\n

\u65b9\u6cd5\u4e8c\uff1ahash\u8868\uff08\u54c8\u5e0c\u8868\uff0c\u6839\u636ekey value\u76f4\u63a5\u8bbf\u95ee\u7684\u6570\u636e\u7ed3\u6784\uff09<\/p>\n

var arr = [3, 3, 5, 5, 7, 1, 8, 8];\nfunction removeRepeat(){  \n      var h={};       \/\/\u5b9a\u4e49\u4e00\u4e2ahash\u8868 \n      var newArr=[];  \/\/\u5b9a\u4e49\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4  \n      for(var i = 0; i < arr.length; i++){    \/\/\u5faa\u73af\u904d\u5386\u5f53\u524d\u6570\u7ec4  \n            \/\/\u5bf9\u5143\u7d20\u8fdb\u884c\u5224\u65ad\uff0c\u770b\u662f\u5426\u5df2\u7ecf\u5b58\u5728hash\u8868\u4e2d\uff0c\u82e5\u5b58\u5728\u5219\u8df3\u8fc7\uff0c\u5426\u5219\u5b58\u5165\u4e34\u65f6\u6570\u7ec4  \n            if(!h[arr[i]]){  \n                \/\/\u5b58\u5165hash\u8868  \n                h[arr[i]] = true;  \n                \/\/\u628a\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u5b58\u5165\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d  \n                newArr.push(arr[i]);  \n            }  \n       }  \n       return newArr;  \n   }<\/code><\/pre>\n

\u65b9\u6cd5\u4e09\uff1a\u6392\u5e8f\uff0c\u6bd4\u8f83\u76f8\u90bb\u4e24\u4e2a<\/p>\n

var arr = [3, 3, 5, 5, 7, 1, 8, 8];\nfunction removeRepeat(){ \n      \/\/\u5c06\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f  \n      this.sort();  \n      \/\/\u5b9a\u4e49\u7ed3\u679c\u6570\u7ec4  \n      var newArr=[];  \n      for(var i = 1; i < arr.length; i++){    \/\/\u4ece\u6570\u7ec4\u7b2c\u4e8c\u9879\u5f00\u59cb\u5faa\u73af\u904d\u5386\u6570\u7ec4  \n           \/\/\u5224\u65ad\u76f8\u90bb\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\u8bf4\u660e\u6570\u636e\u91cd\u590d\uff0c\u5426\u5219\u5c06\u5143\u7d20\u5199\u5165\u7ed3\u679c\u6570\u7ec4  \n           if(arr[i] !== newArr[newArr.length - 1]){  \n               newArr.push(this[i]);  \n           }              \n      }  \n      return newArr;  \n              \n  } <\/code><\/pre>\n

\u65b9\u6cd5\u56db\uff1aSet\u5bf9\u8c61\uff0c[...]\u89e3\u6784<\/p>\n

var arr = [3, 3, 5, 5, 7, 1, 8, 8];\nvar mySet = new Set(arr);\nvar newArr = [...mySet];<\/code><\/pre>\n

\u65b9\u6cd5\u4e94\uff1aArray.from()<\/strong><\/font>\u65b9\u6cd5\u5bf9\u4e00\u4e2a\u7c7b\u4f3c\u6570\u7ec4\u6216\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\uff0c\u6d45\u62f7\u8d1d\u7684\u6570\u7ec4\u5b9e\u4f8b\u3002<\/p>\n

\nvar arr = [3, 3, 5, 5, 7, 1, 8, 8];\nvar mySet = new Set(arr);\nvar newArr = Array.from(mySet);<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"\u6570\u7ec4\u53bb\u91cd\u7684\u65b9\u6cd5\u5df2\u77e5\u6570\u7ec4arr=[3,3,5,5,7,1,8,8]\uff0c\u53bb\u6389\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u6570\uff0c\u5f97\u5230newArr=[3,5,7,1,8]\u65b9\u6cd5\u4e00\uff1aindexOfvararr=[3,3,5,5...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/607"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=607"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}