<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>数组去重</title> </head> <body> <script> // 数组去重 // 方法一(原理性): var arr = [2, 5, 12, 21, 54, 22, 61, 6, 1, 72, 4, 1, 3, 5, 3, 51, 4, 2, 22] var result = [] for (var i = 0; i < arr.length; i++) { var flag = true for (var j = 0; j < result.length; j++) { if (arr[i] === result[j]) { flag = false break } } if (flag === true) { result[result.length] = arr[i] } } // 方法二: // var result = new Set(arr) console.log(result) </script> </body> </html>