{"id":589,"date":"2024-01-08T09:01:01","date_gmt":"2024-01-08T01:01:01","guid":{"rendered":""},"modified":"2024-01-08T09:01:01","modified_gmt":"2024-01-08T01:01:01","slug":"js\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 \u603b\u7ed3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/589.html","title":{"rendered":"js\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 \u603b\u7ed3"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4f7f\u7528indexOf()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n

indexOf\u65b9\u6cd5\u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u7ed3\u679c\u662f\u5339\u914d\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u5219\u8fd4\u56de-1<\/p>\n

var arr = [1,1,2,2,3,3,4,4];\nvar newArr = [];\nfor(var i=0,len=arr.length;i<len;i++){\nif(newArr.indexOf(arr[i]) == -1){\nnewArr.push(arr[i]);\n}\n}\nconsole.log(newArr);\n<\/code><\/pre>\n
\u4f7f\u7528lastIndexOf()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
    \n
  • lastIndexOf\u65b9\u6cd5\u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u5219\u8fd4\u56de-1<\/li>\n
  • lastIndexOf\u65b9\u6cd5\u7684\u7528\u6cd5\u8ddfindexOf\u65b9\u6cd5\u4e00\u81f4\uff0c\u4e3b\u8981\u7684\u533a\u522blastIndexOf\u4ece\u5c3e\u90e8\u5f00\u59cb\u5339\u914d\uff0cindexOf\u5219\u662f\u4ece\u5934\u90e8\u5f00\u59cb\u5339\u914d<\/li>\n
  • \u53e6\u5916\uff0clastIndexOf\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u4ece\u8be5\u4f4d\u7f6e\u8d77\u5411\u524d\u5339\u914d<\/li>\n<\/ul>\n
    function noRepeat(arr) {\nvar res = [];\nfor (var i = 0; i < arr.length; i++) {\n    res.lastIndexOf(arr[i]) !== -1 ? '' : res.push(arr[i]);\n}\nreturn res;\n}\nconsole.log(noRepeat([1, 1, 2, 2, 3, 3, 4, 4]));\n<\/code><\/pre>\n
    \u4f7f\u7528filter()\u4e0eindexOf()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
      \n
    • filter\u65b9\u6cd5\u7528\u4e8e\u8fc7\u6ee4\u6570\u7ec4\u6210\u5458\uff0c\u6ee1\u8db3\u6761\u4ef6\u7684\u6210\u5458\u7ec4\u6210\u4e00\u4e2a\u65b0\u6570\u7ec4\u8fd4\u56de<\/li>\n
    • \u5b83\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6240\u6709\u6570\u7ec4\u6210\u5458\u4f9d\u6b21\u6267\u884c\u8be5\u51fd\u6570\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3atrue\u7684\u6210\u5458\u7ec4\u6210\u4e00\u4e2a\u65b0\u6570\u7ec4\u8fd4\u56de<\/li>\n
    • \u8be5\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u539f\u6570\u7ec4<\/li>\n
    • filter\u65b9\u6cd5\u7684\u53c2\u6570\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u5f53\u524d\u6210\u5458\uff0c\u5f53\u524d\u4f4d\u7f6e\u548c\u6574\u4e2a\u6570\u7ec4<\/li>\n
    • filter()\u53bb\u9664\u91cd\u590d\u5143\u7d20\u4f9d\u9760\u7684\u662findexOf\u7684\u5c5e\u6027\uff0c\u5373\u603b\u662f\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u540e\u9762\u7684\u91cd\u590d\u5143\u7d20\u4f4d\u7f6e\u4e0eindexOf\u8fd4\u56de\u7684\u4f4d\u7f6e\u4e0d\u76f8\u7b49\uff0c\u6240\u4ee5filter()\u53ef\u4ee5\u8fc7\u6ee4\u6389\u5b83\u4eec<\/li>\n<\/ul>\n
      var arr = [1,1,2,2,3,3,4,4];\n    var newArr = arr.filter(function(value,index,self){\n    return self.indexOf(value) === index;\n});\nconsole.log(newArr)\n<\/code><\/pre>\n
      \u4f7f\u7528ES6\u63d0\u4f9b\u7684Set\u7ed3\u6784\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
        \n
      • \u4e3b\u8981\u5229\u7528Set\u7ed3\u6784\u4e0d\u80fd\u63a5\u6536\u91cd\u590d\u6570\u636e\u7684\u7279\u70b9<\/li>\n<\/ul>\n
        var arr = [1,1,2,2,3,3,4,4];\n    function noRepeat(arr){\n        var newArr = [];\n        var myset = new Set(arr);\n        for(var val of myset){\n        newArr.push(val);\n}\n    return newArr;\n}\nvar arr2 = noRepeat(arr);\nconsole.log(arr2)\n<\/code><\/pre>\n
        \u4f7f\u7528ES6\u63d0\u4f9b\u7684Set\u4e0e\u6269\u5c55\u8fd0\u7b97\u7b26\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
        var arr = [1,1,2,2,3,3,4,4];\nfunction norepeat(arr) {\n    let set = new Set(arr);\n    arr = [...set];\n    return arr;\n}\nconsole.log(norepeat(arr))\n<\/code><\/pre>\n
        \u4f7f\u7528set\u4e0eArray.from()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
          \n
        • Array.from\u65b9\u6cd5\u53ef\u4ee5\u5c06 Set \u7ed3\u6784\u8f6c\u4e3a\u6570\u7ec4<\/li>\n<\/ul>\n
          var arr = [1,2,2,2,3,3,4,4];\nvar newArr = Array.from(new Set(arr));\nconsole.log(newArr);\n<\/code><\/pre>\n
          \u4f7f\u7528splice()\u65b9\u6cd5\u4e0e\u53cc\u5c42\u5faa\u73af\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
            \n
          1. splice()\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u539f\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u6210\u5458\uff0c\u5e76\u53ef\u4ee5\u5728\u5220\u9664\u7684\u4f4d\u7f6e\u6dfb\u52a0\u65b0\u7684\u6570\u7ec4\u6210\u5458\uff0c\u8fd4\u56de\u503c\u662f\u88ab\u5220\u9664\u7684\u5143\u7d20<\/li>\n<\/ol>\n
            var arr = [1,1,2,2,3,3,4,4];\nfunction noRepeat(arr){\n    for (var i = 0; i < arr.length; i++) {\n        for (var j = 0; j < arr.length; j++) {\n            if (arr[i] == arr[j] && i != j) {\n                arr.splice(j, 1);\n            }\n        }\n    }\n    return arr;\n}\nvar arr2  = noRepeat(arr);\nconsole.log(arr2)\n<\/code><\/pre>\n
            \u4f7f\u7528includes()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
              \n
            • includes()\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u627e\u5230\u5339\u914d\u7684\u5b57\u7b26\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002<\/li>\n<\/ul>\n
              function noRepeat(arr) {\n    var newArr = [];\n    for (var i = 0; i < arr.length; i++) {\n        if (!newArr.includes(arr[i])) {\n            newArr.push(arr[i]);\n        }\n    }\n    return newArr;\n}\nconsole.log(noRepeat([1,1,2,2,3,3,4,4]))\n<\/code><\/pre>\n
              \u4f7f\u7528forEach()\u548cincludes()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
                \n
              • forEach\u65b9\u6cd5\u662f\u5bf9\u6570\u7ec4\u7684\u6240\u6709\u6210\u5458\u4f9d\u6b21\u6267\u884c\u53c2\u6570\u51fd\u6570\u3002\u4f46\u662f\uff0cforEach\u65b9\u6cd5\u4e0d\u8fd4\u56de\u503c\uff0c\u53ea\u7528\u6765\u64cd\u4f5c\u6570\u636e<\/li>\n
              • forEach\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u540c\u6837\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u5f53\u524d\u503c\u3001\u5f53\u524d\u4f4d\u7f6e\u3001\u6574\u4e2a\u6570\u7ec4<\/li>\n<\/ul>\n
                function noRepeat(arr) {\n    var newArr = [];\n    arr.forEach(item => {\n        return newArr.includes(item) ? '' : newArr.push(item)\n    });\n    return newArr;\n}\nconsole.log(noRepeat([1,1,2,2,3,3,4,4]));\n<\/code><\/pre>\n
                \u5229\u7528\u7a7a\u5bf9\u8c61\u6765\u8bb0\u5f55\u65b0\u6570\u7ec4\u4e2d\u5df2\u7ecf\u5b58\u50a8\u8fc7\u7684\u5143\u7d20<\/h6>\n
                var arr = [1,1,2,2,3,3,4,4];\nvar obj={};\nvar newArr=[];\nfor(var i=0;i<arr.length;i++){\n    if(!obj[arr[i]]){\n        obj[arr[i]]=true;\n        newArr.push(arr[i]);\n    }\n}\nconsole.log(newArr);\n<\/code><\/pre>\n
                \u4f7f\u7528sort()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n
                  \n
                1. \u4f7f\u7528sort()\u65b9\u6cd5\u5148\u5c06\u539f\u6570\u7ec4\u6392\u5e8f\uff0c\u7136\u540e\u4e0e\u76f8\u90bb\u7684\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u4e0d\u540c\u5219\u5b58\u5165\u65b0\u6570\u7ec4\u3002\uff08\u539f\u6570\u7ec4\u957f\u5ea6\u4e0d\u53d8\uff0c\u4f46\u8981\u6ce8\u610f\uff1asort\u65b9\u6cd5\u4e0d\u662f\u6309\u7167\u5927\u5c0f\u6392\u5e8f\uff0c\u800c\u662f\u6309\u7167\u5b57\u5178\u987a\u5e8f\uff09<\/li>\n<\/ol>\n
                  var arr = [1,1,2,2,3,3,4,4];\nfunction noRepeat(arr) {\n    var ret = [];\n    arr.sort();  \n    var res = arr[0];\n    ret.push(arr[0]);\n    for (var i = 1; i < arr.length; i++) {\n        if (arr[i] != res) { \n            ret.push(arr[i]);\n            res = arr[i];\n        }\n    }\n    return ret;\n}\nvar arr2 = noRepeat(arr);\nconsole.log(arr2);\n<\/code><\/pre>\n
                  \u6765\u4e86\u5c31\u70b9\u4e2a\u8d5e\u518d\u8d70\u628a (* \uffe33)(\u03b5\uffe3 *)d<\/h5>\n","protected":false},"excerpt":{"rendered":"js\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 \u603b\u7ed3\u4f7f\u7528indexOf()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20indexOf\u65b9\u6cd5\u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u7ed3\u679c\u662f\u5339\u914d\u5f00\u59cb...","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\/589"}],"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=589"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}