-
Notifications
You must be signed in to change notification settings - Fork 913
/
数组去重.html
53 lines (52 loc) · 1.56 KB
/
数组去重.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>窗口登陆效果-蚂蚁部落</title>
<script type="text/javascript">
Array.prototype.deleteEle=function()
{
var obj={}; //建立一个空对象
var newArr=[];
var i,j;
for(i=0;i<this.length;i++)
{
if(typeof(obj[this[i]])=="undefined") //将数组里面的元素作为属性名,如果obj不包含属性名,则建立属性名:''这样的json键值对(如'1':''),经过该for循环之后obj={'1':'','2':'','3':'','4':'','5':'','6':'','8':''}。其实这一步已经去重了。
{
obj[this[i]]="";
}
}
for(j in obj)
{
newArr.push(j); //将不重复的属性名添加到新数组里面并返回值
}
return newArr;
};
var theArray=new Array(1,2,3,4,5,6,8,5,4);
document.write(theArray.deleteEle());
// 不用原型
function test(arr)
{
var obj={};
var newArr=[];
var i,j;
for(i=0;i<arr.length;i++)
{
if(typeof(obj[arr[i]])=="undefined") //
{
obj[arr[i]]="";
}
}
for(j in obj)
{
newArr.push(j);
}
return newArr;
}
var test1=test(['1','3','5','123123','2','3']);
console.log(test1);
</script>
</head>
<body >
</body>
</html>