{"id":608,"date":"2023-12-29T18:01:01","date_gmt":"2023-12-29T10:01:01","guid":{"rendered":""},"modified":"2023-12-29T18:01:01","modified_gmt":"2023-12-29T10:01:01","slug":"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168","status":"publish","type":"post","link":"https:\/\/mushiming.com\/608.html","title":{"rendered":"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168"},"content":{"rendered":"

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

\n

\u5199\u5728\u524d\u9762\uff1a<\/h3>\n

\u6211\u4eec\u8981\u60f3\u4f7f\u7528\u6570\u7ec4\u53bb\u91cd\uff0c\u90a3\u5c31\u5fc5\u987b\u5bf9\u6570\u7ec4\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u5173\u4e8eJS\u6570\u7ec4\u7684\u4e00\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u4f7f\u7528\uff0c\u53ef\u53c2\u8003\uff1a
\u2460JavaScript \u5185\u7f6e\u5bf9\u8c61\u4e4b-Array
\u2461ES5\u65b0\u589e\u6570\u7ec4\u65b9\u6cd5
\u2462\u6d45\u8c08JavaScript\u4e2dES6\u65b0\u589e\u7684Set\u548cMap\u7ed3\u6784\u4ee5\u53caArray.from\u65b9\u6cd5<\/strong><\/p>\n<\/blockquote>\n


\n

\u4e0b\u9762\u8fdb\u5165\u6b63\u9898\uff1a\u5229\u7528JS\u7684\u6570\u7ec4\u5b9e\u73b0\u53bb\u91cd\u7684\u76ee\u7684<\/code><\/h4>\n

        JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u76f8\u4fe1\u4e00\u4e9b\u5c0f\u4f19\u4f34\u4e5f\u638c\u63e1\u4e86\u597d\u591a\u79cd\u65b9\u6cd5\uff0c\u90a3\u63a5\u4e0b\u6765\u6211\u5c31\u4ecb\u7ecd\u4e00\u4e9b\u6bd4\u8f83\u5e38\u7528\u7684\u65b9\u6cd5\u4f9b\u5927\u5bb6\u53c2\u8003\u3002<\/p>\n

\u65b9\u6cd51\uff1a\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

\u4f8b\uff1a<\/p>\n

var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\nvar<\/span> newArr =<\/span> [<\/span>]<\/span>;<\/span>\nfor<\/span>(<\/span>var<\/span> i=<\/span>0<\/span>,<\/span>len=<\/span>arr.<\/span>length;<\/span>i<<\/span>len;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\nif<\/span>(<\/span>newArr.<\/span>indexOf<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span> ==<\/span> -<\/span>1<\/span>)<\/span>{ \n   <\/span>\nnewArr.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n}<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>newArr)<\/span>;<\/span>\n<\/code><\/pre>\n

\u7ed3\u679c\uff1a
\"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

\u65b9\u6cd52\uff1a\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\uff1b<\/li>\n
  • \u53e6\u5916\uff0clastIndexOf\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u4ece\u8be5\u4f4d\u7f6e\u8d77\u5411\u524d\u5339\u914d\u3002<\/li>\n<\/ul>\n

    \u4f8b\uff1a<\/p>\n

    function<\/span> noRepeat<\/span>(<\/span>arr)<\/span> { \n   <\/span>\nvar<\/span> res =<\/span> [<\/span>]<\/span>;<\/span>\nfor<\/span> (<\/span>var<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n    res.<\/span>lastIndexOf<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span> !==<\/span> -<\/span>1<\/span> ?<\/span> ''<\/span> :<\/span> res.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n}<\/span>\nreturn<\/span> res;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>noRepeat<\/span>(<\/span>[<\/span>1<\/span>,<\/span> 1<\/span>,<\/span> 2<\/span>,<\/span> 2<\/span>,<\/span> 3<\/span>,<\/span> 3<\/span>,<\/span> 4<\/span>,<\/span> 4<\/span>]<\/span>)<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

    \u7ed3\u679c\uff1a
    \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

    \u65b9\u6cd53\uff1a\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\uff1b<\/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\uff1b<\/li>\n
    • \u8be5\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u539f\u6570\u7ec4\uff1b<\/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\uff1b<\/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\u3002<\/li>\n<\/ul>\n

      \u4f8b\uff1a<\/p>\n

      var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\n    var<\/span> newArr =<\/span> arr.<\/span>filter<\/span>(<\/span>function<\/span>(<\/span>value,<\/span>index,<\/span>self)<\/span>{ \n   <\/span>\n    return<\/span> self.<\/span>indexOf<\/span>(<\/span>value)<\/span> ===<\/span> index;<\/span>\n}<\/span>)<\/span>;<\/span>\nconsole.<\/span>log<\/span>(<\/span>newArr)<\/span>;<\/span>\n<\/code><\/pre>\n

      \u7ed3\u679c\uff1a
      \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

      \u65b9\u6cd54\uff1a\u4f7f\u7528ES6\u63d0\u4f9b\u7684Set\u7ed3\u6784\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n

              \u4e3b\u8981\u5229\u7528Set\u7ed3\u6784\u4e0d\u80fd\u63a5\u6536\u91cd\u590d\u6570\u636e\u7684\u7279\u70b9\u3002<\/p>\n

      \u4f8b\uff1a<\/p>\n

      var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\n    function<\/span> noRepeat<\/span>(<\/span>arr)<\/span>{ \n   <\/span>\n        var<\/span> newArr =<\/span> [<\/span>]<\/span>;<\/span>\n        var<\/span> myset =<\/span> new<\/span> Set<\/span>(<\/span>arr)<\/span>;<\/span>\n        for<\/span>(<\/span>var<\/span> val of<\/span> myset)<\/span>{ \n   <\/span>\n        newArr.<\/span>push<\/span>(<\/span>val)<\/span>;<\/span>\n}<\/span>\n    return<\/span> newArr;<\/span>\n}<\/span>\nvar<\/span> arr2 =<\/span> noRepeat<\/span>(<\/span>arr)<\/span>;<\/span>\nconsole.<\/span>log<\/span>(<\/span>arr2)<\/span>;<\/span>\n<\/code><\/pre>\n

      \u7ed3\u679c\uff1a
      \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

      \u65b9\u6cd55\uff1a\u4f7f\u7528ES6\u63d0\u4f9b\u7684Set\u4e0e\u6269\u5c55\u8fd0\u7b97\u7b26\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n

              ES6\u7684\u51fa\u73b0\uff0c\u771f\u771f\u662f\u6781\u65b9\u4fbf\u7684~~~<\/p>\n

      \u4f8b\uff1a<\/p>\n

      var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\nfunction<\/span> norepeat<\/span>(<\/span>arr)<\/span> { \n   <\/span>\n    let<\/span> set<\/span> =<\/span> new<\/span> Set<\/span>(<\/span>arr)<\/span>;<\/span>\n    arr =<\/span> [<\/span>...<\/span>set<\/span>]<\/span>;<\/span>\n    return<\/span> arr;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>norepeat<\/span>(<\/span>arr)<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

      \u7ed3\u679c\uff1a
      \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

      \u65b9\u6cd56\uff1a\u4f7f\u7528set\u4e0eArray.from()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n

              Array.from\u65b9\u6cd5\u53ef\u4ee5\u5c06 Set \u7ed3\u6784\u8f6c\u4e3a\u6570\u7ec4\u3002<\/p>\n

      \u4f8b\uff1a<\/p>\n

      var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\nvar<\/span> newArr =<\/span> Array.<\/span>from<\/span>(<\/span>new<\/span> Set<\/span>(<\/span>arr)<\/span>)<\/span>;<\/span>\nconsole.<\/span>log<\/span>(<\/span>newArr)<\/span>;<\/span>\n<\/code><\/pre>\n

      \u7ed3\u679c\uff1a
      \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

      \u65b9\u6cd57\uff1a\u4f7f\u7528splice()\u65b9\u6cd5\u4e0e\u53cc\u5c42\u5faa\u73af\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\n

              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\u3002<\/p>\n

      \u4f8b\uff1a<\/p>\n

      var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\nfunction<\/span> noRepeat<\/span>(<\/span>arr)<\/span>{ \n   <\/span>\n    for<\/span> (<\/span>var<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n        for<\/span> (<\/span>var<\/span> j =<\/span> 0<\/span>;<\/span> j <<\/span> arr.<\/span>length;<\/span> j++<\/span>)<\/span> { \n   <\/span>\n            if<\/span> (<\/span>arr[<\/span>i]<\/span> ==<\/span> arr[<\/span>j]<\/span> &&<\/span> i !=<\/span> j)<\/span> { \n   <\/span>\n                arr.<\/span>splice<\/span>(<\/span>j,<\/span> 1<\/span>)<\/span>;<\/span>\n            }<\/span>\n        }<\/span>\n    }<\/span>\n    return<\/span> arr;<\/span>\n}<\/span>\nvar<\/span> arr2  =<\/span> noRepeat<\/span>(<\/span>arr)<\/span>;<\/span>\nconsole.<\/span>log<\/span>(<\/span>arr2)<\/span>;<\/span>   \n<\/code><\/pre>\n

      \u7ed3\u679c\uff1a
      \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

      \u65b9\u6cd58\uff1a\u4f7f\u7528includes()\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20<\/h6>\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<\/p>\n

      \u4f8b\uff1a<\/p>\n

      function<\/span> noRepeat<\/span>(<\/span>arr)<\/span> { \n   <\/span>\n    var<\/span> newArr =<\/span> [<\/span>]<\/span>;<\/span>\n    for<\/span> (<\/span>var<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>!<\/span>newArr.<\/span>includes<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>)<\/span> { \n   <\/span>\n            newArr.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n    return<\/span> newArr;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>noRepeat<\/span>(<\/span>[<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>)<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

      \u7ed3\u679c\uff1a
      \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

      \u65b9\u6cd59\uff1a\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\u3002<\/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\u3002<\/li>\n<\/ul>\n

        \u4f8b\uff1a<\/p>\n

        function<\/span> noRepeat<\/span>(<\/span>arr)<\/span> { \n   <\/span>\n    var<\/span> newArr =<\/span> [<\/span>]<\/span>;<\/span>\n    arr.<\/span>forEach<\/span>(<\/span>item =><\/span> { \n   <\/span>\n        return<\/span> newArr.<\/span>includes<\/span>(<\/span>item)<\/span> ?<\/span> ''<\/span> :<\/span> newArr.<\/span>push<\/span>(<\/span>item)<\/span>\n    }<\/span>)<\/span>;<\/span>\n    return<\/span> newArr;<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>noRepeat<\/span>(<\/span>[<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>)<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        \u7ed3\u679c\uff1a
        \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

        \u65b9\u6cd510\uff1a\u5229\u7528\u7a7a\u5bf9\u8c61\u6765\u8bb0\u5f55\u65b0\u6570\u7ec4\u4e2d\u5df2\u7ecf\u5b58\u50a8\u8fc7\u7684\u5143\u7d20<\/h6>\n

        \u4f8b\uff1a<\/p>\n

        var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\nvar<\/span> obj=<\/span>{ \n   <\/span>}<\/span>;<\/span>\nvar<\/span> newArr=<\/span>[<\/span>]<\/span>;<\/span>\nfor<\/span>(<\/span>var<\/span> i=<\/span>0<\/span>;<\/span>i<<\/span>arr.<\/span>length;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\n    if<\/span>(<\/span>!<\/span>obj[<\/span>arr[<\/span>i]<\/span>]<\/span>)<\/span>{ \n   <\/span>\n        obj[<\/span>arr[<\/span>i]<\/span>]<\/span>=<\/span>true<\/span>;<\/span>\n        newArr.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\nconsole.<\/span>log<\/span>(<\/span>newArr)<\/span>;<\/span> \n<\/code><\/pre>\n

        \u7ed3\u679c\uff1a
        \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n

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

                \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<\/p>\n

        \u4f8b\uff1a<\/p>\n

        var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>4<\/span>]<\/span>;<\/span>\nfunction<\/span> noRepeat<\/span>(<\/span>arr)<\/span> { \n   <\/span>\n    var<\/span> ret =<\/span> [<\/span>]<\/span>;<\/span>\n    arr.<\/span>sort<\/span>(<\/span>)<\/span>;<\/span>  \n    var<\/span> res =<\/span> arr[<\/span>0<\/span>]<\/span>;<\/span>\n    ret.<\/span>push<\/span>(<\/span>arr[<\/span>0<\/span>]<\/span>)<\/span>;<\/span>\n    for<\/span> (<\/span>var<\/span> i =<\/span> 1<\/span>;<\/span> i <<\/span> arr.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>arr[<\/span>i]<\/span> !=<\/span> res)<\/span> { \n   <\/span> \n            ret.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>;<\/span>\n            res =<\/span> arr[<\/span>i]<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n    return<\/span> ret;<\/span>\n}<\/span>\nvar<\/span> arr2 =<\/span> noRepeat<\/span>(<\/span>arr)<\/span>;<\/span>\nconsole.<\/span>log<\/span>(<\/span>arr2)<\/span>;<\/span> \n<\/code><\/pre>\n

        \u7ed3\u679c\uff1a
        \"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168<\/p>\n


        \n

        \u9119\u4eba\u624d\u758f\u5b66\u6d45\uff0c\u5982\u6709\u7eb0\u6f0f\uff0c\u671b\u5404\u8def\u5de8\u4f6c\u4e0d\u541d\u8d50\u6559~<\/code><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"\u5e38\u7528\u7684JS\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5927\u5168\u5199\u5728\u524d\u9762\uff1a\u6211\u4eec\u8981\u60f3\u4f7f\u7528\u6570\u7ec4\u53bb\u91cd\uff0c\u90a3\u5c31\u5fc5\u987b\u5bf9\u6570\u7ec4\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u5173\u4e8eJS\u6570\u7ec4\u7684\u4e00\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u4f7f\u7528\uff0c\u53ef...","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\/608"}],"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=608"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/608\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}