{"id":588,"date":"2024-01-08T18:01:01","date_gmt":"2024-01-08T10:01:01","guid":{"rendered":""},"modified":"2024-01-08T18:01:01","modified_gmt":"2024-01-08T10:01:01","slug":"JavaScript\u4e24\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/588.html","title":{"rendered":"JavaScript\u4e24\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5"},"content":{"rendered":"

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

JavaScript\u4e24\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5<\/h2>\n

\u7b2c\u4e00\u79cd<\/strong>\uff08\u5229\u7528splice()\u65b9\u6cd5\u53bb\u91cd\uff09<\/p>\n

splice() \u65b9\u6cd5\u7528\u4e8e\u63d2\u5165\u3001\u5220\u9664\u6216\u66ff\u6362\u6570\u7ec4\u7684\u5143\u7d20\u3002<\/mark><\/p>\n

   \/\/ \u7533\u660e\u4e00\u4e2a\u6570\u7ec4<\/span>\n    var   arr =<\/span> [<\/span>1<\/span>,<\/span>3<\/span>,<\/span>5<\/span>,<\/span>4<\/span>,<\/span>3<\/span>,<\/span>1<\/span>,<\/span>5<\/span>,<\/span>7<\/span>,<\/span>8<\/span>,<\/span>4<\/span>,<\/span>7<\/span>]<\/span>;<\/span>\n\n    function temp<\/span>(<\/span>arr)<\/span>{ \n   <\/span>\n        for<\/span>(<\/span>i=<\/span>0<\/span>;<\/span>i<<\/span>arr.<\/span>length;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\n            for<\/span>(<\/span>j=<\/span>i+<\/span>1<\/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>{ \n   <\/span>        \n                    arr.<\/span>splice<\/span>(<\/span>j,<\/span>1<\/span>)<\/span>;<\/span>      \n                    \/\/ \u5f53\u7b2c\u4e00\u4e3a\u6570\u4e8e\u7b2c\u4e8c\u4f4d\u6570\u76f8\u540c\u65f6 \u88c1\u526a\u6389\u7b2c\u4e8c\u4f4d\u6570 <\/span>\n                    j--<\/span>;<\/span>     \/\/\u539f\u5730\u5224\u65ad\u4e00\u6b21 \u9632\u6b62\u9057\u6f0f<\/span>\n                }<\/span>\n            }<\/span>\n        }<\/span>\n        return<\/span> arr;<\/span>\t\t\t\n    }<\/span>\n    console.<\/span>log<\/span>(<\/span>temp<\/span>(<\/span>arr)<\/span>)<\/span>;<\/span>\t\/\/(6) [1, 3, 5, 4, 7, 8]<\/span>\n<\/code><\/pre>\n

\u7b2c\u4e8c\u79cd<\/strong>\uff08\u5229\u7528indexOf()\u65b9\u6cd5\u53bb\u91cd\uff09<\/p>\n

indexOf() \u65b9\u6cd5\u53ef\u8fd4\u56de\u67d0\u4e2a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u503c\u5728\u5b57\u7b26\u4e32\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002\u5f53\u76ee\u6807\u503c\u5b58\u5728\u6570\u7ec4\u65f6\u8fd4\u56de\u5176\u4e0b\u6807\uff0c\u4e0d\u5b58\u5728\u65f6\u653e\u56de-1\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd<\/mark><\/p>\n

 function temp2<\/span>(<\/span>arr)<\/span>{ \n   <\/span>\n        var  wrap =<\/span> [<\/span>]<\/span>;<\/span>                 \/\/\u5efa\u7acb\u7a7a\u6570\u7ec4<\/span>\n        for<\/span>(<\/span>i=<\/span>0<\/span>;<\/span>i<<\/span>arr.<\/span>length;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\n            if<\/span>(<\/span>wrap.<\/span>indexOf<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>==<\/span> -<\/span>1<\/span> )<\/span>{ \n   <\/span>       \n              \/\/ \u5728\u7a7a\u6570\u7ec4\u91cc\u627earr\u6570\u7ec4\u7684\u6570\u636e \u5982\u679c\u6ca1\u6709\u627e\u5230\u5c31\u5728\u7a7a\u6570\u7ec4\u5185\u6dfb\u52a0<\/span>\n                wrap.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>\n            }<\/span>\n        }<\/span>\n        return<\/span> wrap\n    }<\/span>\n\n    console.<\/span>log<\/span>(<\/span>temp2<\/span>(<\/span>arr)<\/span>)<\/span>\t\t\/\/(6) [1, 3, 5, 4, 7, 8]<\/span>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"JavaScript\u4e24\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5JavaScript\u4e24\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u7b2c\u4e00\u79cd\uff08\u5229\u7528splice\u65b9\u6cd5\u53bb\u91cd\uff09splice()\u65b9\u6cd5\u7528\u4e8e\u63d2\u5165\u3001\u5220\u9664\u6216\u66ff\u6362\u6570\u7ec4...","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\/588"}],"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=588"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/588\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}