{"id":591,"date":"2024-01-07T09:01:01","date_gmt":"2024-01-07T01:01:01","guid":{"rendered":""},"modified":"2024-01-07T09:01:01","modified_gmt":"2024-01-07T01:01:01","slug":"\u6700\u5e38\u7528\u7684\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/591.html","title":{"rendered":"\u6700\u5e38\u7528\u7684\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5"},"content":{"rendered":"

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

\u6574\u7406\u4e86\u4e00\u4e0b\u6211\u6700\u5e38\u7528\u7684\u4e09\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\uff0c\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801<\/p>\n

1\u3001indexOf()\u65b9\u6cd5\u8fd4\u56de\u5728\u6570\u7ec4\u4e2d\u53ef\u4ee5\u627e\u5230\u4e00\u4e2a\u7ed9\u5b9a\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de-1<\/h2>\n
\/\/ \u7b2c\u4e00\u79cd\u65b9\u6cd5 indexOf<\/span>\n    function<\/span> test<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n        let<\/span> newArr =<\/span> [<\/span>]<\/span>\n        for<\/span>(<\/span>let<\/span> i=<\/span>0<\/span>;<\/span>i<<\/span>arr.<\/span>length;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\n            \/\/ \u5982\u679c\u8be5\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u627e\u4e0d\u5230 \u5c31\u4f1a\u8fd4\u56de-1<\/span>\n            if<\/span>(<\/span>newArr.<\/span>indexOf<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>===<\/span>-<\/span>1<\/span>)<\/span>{ \n   <\/span>\n                newArr.<\/span>push<\/span>(<\/span>arr[<\/span>i]<\/span>)<\/span>\n            }<\/span>\n        }<\/span>\n        return<\/span> newArr\n    }<\/span>\n    const<\/span> arr =<\/span> test<\/span>(<\/span>[<\/span>1<\/span>,<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>1<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>2<\/span>]<\/span>)<\/span>\n    console.<\/span>log<\/span>(<\/span>arr)<\/span>;<\/span>\n<\/code><\/pre>\n

2\u3001splice(index, n, item1, item2,\u2026) \u4ece\u7d22\u5f15 index \u7684\u4f4d\u7f6e\u5f00\u59cb\u5220\u9664 n \u4e2a\u5143\u7d20\uff0c\u63d2\u5165\u201citem1\u201d \u201citem2\u201d\u2026<\/h2>\n

\uff08\u6ce8\u610f\uff1a\u63d2\u5165\u5143\u7d20\u7684\u6570\u91cf\u53ef\u4ee5\u662f\u591a\u4e2a\uff0c\u4e5f\u53ef\u4ee5\u662f0\u4e2a\uff0c\u5982\u679c\u662f0\u4e2a\uff0c\u53ef\u4ee5\u7701\u7565\u4e0d\u5199\uff09<\/h2>\n
\/\/ \u7b2c\u4e8c\u79cd\u65b9\u6cd5 splice<\/span>\n    function<\/span> newArr<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n        for<\/span>(<\/span>let<\/span> i=<\/span>0<\/span>;<\/span>i<<\/span>arr.<\/span>length;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\n            for<\/span>(<\/span>let<\/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                    \/\/ \u5982\u679c\u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a\u4e0d\u540c\u7d22\u5f15\u7684\u503c\u76f8\u540c\uff0c\u5219\u53bb\u6389\u540e\u9762\u90a3\u4e2a<\/span>\n                    arr.<\/span>splice<\/span>(<\/span>j,<\/span>1<\/span>)<\/span>\n                    \/\/ \u6ce8\u610f\u5220\u6389\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\u4e4b\u540e\uff0c\u6570\u7ec4\u957f\u5ea6\u4f1a-1\uff0c<\/span>\n                    \/\/ \u540e\u9762\u503c\u7684\u7d22\u5f15\u4f1a-1\uff0c\u6240\u4ee5j\u4e5f\u9700\u8981-1\uff0c\u4e0d\u7136\u5faa\u73af\u4f1a\u8df3\u8fc7\u4e00\u4e2a\u5143\u7d20<\/span>\n                    j--<\/span>\n                }<\/span>\n            }<\/span>\n        }<\/span>\n        return<\/span> arr\n    }<\/span>\n    const<\/span> arr =<\/span> newArr<\/span>(<\/span>[<\/span>1<\/span>,<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>1<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>2<\/span>]<\/span>)<\/span>\n    console.<\/span>log<\/span>(<\/span>arr)<\/span>;<\/span> \n<\/code><\/pre>\n

3\u3001ES6\u4e2d\u7684Set\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f46\u662f\u6210\u5458\u7684\u503c\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u80fd\u91cd\u590d<\/h2>\n
\/\/ \u7b2c\u4e09\u79cd\u65b9\u6cd5 ES6\u4e2d\u7684Set\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f46\u662f\u6210\u5458\u7684\u503c\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u80fd\u91cd\u590d<\/span>\n    function<\/span> get<\/span>(<\/span>arr<\/span>)<\/span> { \n   <\/span>\n        \/\/ Set\u672c\u8eab\u662f\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u7528\u6765\u751f\u6210Set\u6570\u636e\u7ed3\u6784<\/span>\n        let<\/span> set =<\/span>new<\/span> Set<\/span>(<\/span>arr)<\/span>\n        console.<\/span>log<\/span>(<\/span>set)<\/span>;<\/span>\n        \/\/ Set\u6570\u636e\u7ed3\u6784\u6bd5\u7adf\u4e0d\u662f\u6570\u7ec4\uff0c\u9700\u8981\u7528Array.from()\u8f6c\u5316\u4e3a\u6570\u7ec4<\/span>\n        return<\/span> Array.<\/span>from<\/span>(<\/span>set)<\/span>\n    }<\/span>\n    const<\/span> arr =<\/span> get<\/span>(<\/span>[<\/span>1<\/span>,<\/span>1<\/span>,<\/span>1<\/span>,<\/span>2<\/span>,<\/span>1<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>2<\/span>]<\/span>)<\/span>\n    console.<\/span>log<\/span>(<\/span>arr)<\/span>;<\/span> \n<\/code><\/pre>\n

\u4e0d\u7ba1\u662f\u903b\u8f91\u4e0a\uff0c\u8fd8\u662f\u4ee3\u7801\u957f\u5ea6\u4e0a\uff0cES6\u7684Set\u6570\u636e\u7ed3\u6784\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u90fd\u662f\u6700\u7b80\u6d01\u660e\u4e86\u7684\uff0c\u63a8\u8350\u4f7f\u7528<\/p>\n

\u8865\u5145\u4e00\u4e2areduce\u53bb\u91cd<\/h2>\n
 <<\/span>script><\/span>\n\n        var<\/span> arr =<\/span> [<\/span>1<\/span>,<\/span>1<\/span>,<\/span>1<\/span>,<\/span>3<\/span>,<\/span>2<\/span>,<\/span>5<\/span>,<\/span>4<\/span>,<\/span>3<\/span>,<\/span>5<\/span>]<\/span>\n        const<\/span> arr1 =<\/span> arr.<\/span>reduce<\/span>(<\/span>(<\/span>newArr,<\/span>item<\/span>)<\/span>=><\/span>{ \n   <\/span>\n            newArr.<\/span>includes<\/span>(<\/span>item)<\/span>?<\/span>newArr:<\/span>newArr.<\/span>push<\/span>(<\/span>item)<\/span>\n            return<\/span> newArr\n        }<\/span>,<\/span>[<\/span>]<\/span>)<\/span>\n        console.<\/span>log<\/span>(<\/span>arr1)<\/span>;<\/span>\n        \n    <<\/span>\/<\/span>script><\/span>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u6700\u5e38\u7528\u7684\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u6574\u7406\u4e86\u4e00\u4e0b\u6211\u6700\u5e38\u7528\u7684\u4e09\u79cd\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\uff0c\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u78011\u3001indexOf()\u65b9\u6cd5\u8fd4\u56de\u5728\u6570\u7ec4\u4e2d\u53ef\u4ee5\u627e\u5230\u4e00...","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\/591"}],"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=591"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}