-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_repeat.js
More file actions
51 lines (46 loc) · 959 Bytes
/
Copy pathremove_repeat.js
File metadata and controls
51 lines (46 loc) · 959 Bytes
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
//
//数组去除重复内容
//
Array.prototype.unique = function() {
var json = {};
var res = [];
for(var i = 0;i < this.length;i++) {
if(!json[this[i]]) {
res.push(this[i]);
json[this[i]] = true;
}
}
console.log(json);//{ '0': true, '1': true, '2': true, '3': true, '4': true, '7': true }
return res;
}
var a = [1,2,3,3,2,1,4,4,0,7,7,7,7,7];
console.log(a.unique());
//指定的值找坐标
Array.prototype.valueIndexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
//删除指定的值
Array.prototype.removeValue = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
/*
* 数组去重
*/
function unique(arr) {
var result = [],
hash = {};
for (var i = 0, elem;
(elem = arr[i]) != null; i++) {
if (!hash[elem]) {
result.push(elem);
hash[elem] = true;
}
}
return result;
}