JS数组去重
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用对象方法实现数组去重</title> </head> <body> <script> Array.prototype.unique = function() { var newArr = []; for(var i = 0;i<this.length;i++) { if(newArr.indexOf(this[i]) == -1) { newArr.push(this[i]); } } return newArr; }; var arr = [1,3,1,2]; console.log(arr.unique()); </script> </body> </html>
作者:十八般武藝
出处:http://www.cnblogs.com/terry6
本文版权归属作者和博客园共有,转载请注明出处