数组去除重复的几个方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组去除重复的几个方法</title> </head> <body> </body> <script type="text/javascript"> //利用 ES6的set 方法 var arr = [1, 3, 5, 5, 7, 8, 7, 10, 10, 1 , 3]; console.log(Array.from(new Set(arr))); //利用数组的indexOf下标属性来查询。 var Arr = [1,1,2,3,5,3,1,5,6,7,4]; function unique(arr){ var res = []; for(var i=0; i<arr.length; i++){ if(res.indexOf(arr[i]) == -1){ res.push(arr[i]); } } return res; } console.log(unique(Arr)); var array = [1,10,10,1]; var emp = function unique(arr){ var res = []; var json = []; for (var i = 0; i < this.length; i++) { if(!json[this[i]]){ res.push(this[i]); json[this[i]] = 1; } return res; } } emp(); console.log(unique(arr)); </script> </html><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组去除重复的几个方法</title> </head> <body> </body> <script type="text/javascript"> //利用 ES6的set 方法 var arr = [1, 3, 5, 5, 7, 8, 7, 10, 10, 1 , 3]; console.log(Array.from(new Set(arr))); //利用数组的indexOf下标属性来查询。 var Arr = [1,1,2,3,5,3,1,5,6,7,4]; function unique(arr){ var res = []; for(var i=0; i<arr.length; i++){ if(res.indexOf(arr[i]) == -1){ res.push(arr[i]); } } return res; } console.log(unique(Arr)); var array = [1,10,10,1]; var emp = function unique(arr){ var res = []; var json = []; for (var i = 0; i < this.length; i++) { if(!json[this[i]]){ res.push(this[i]); json[this[i]] = 1; } return res; } } emp(); console.log(unique(arr)); </script> </html>