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>

  

posted on 2017-05-07 21:20  十八般武艺  阅读(90)  评论(0编辑  收藏  举报