js数组去重的两个方法
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
</head
>
<body
>
<script
>
var arr
= [1, 2, 3, 456, 12, 2, 2, 3, 5, 6, 77, 7, 7, 7, 1, 3, 3, 9];
var arr1
= [];
for (var i
= 0; i
< arr
.length
; i
++) {
var idx
= arr1
.indexOf(arr
[i
])
if (idx
== -1) {
arr1
.push(arr
[i
])
}
}
console
.log(arr1
);
for (var j
= 0; j
< arr
.length
; j
++) {
var item
= arr
[j
];
for (var i
= j
+ 1; i
< arr
.length
; i
++) {
if (item
=== arr
[i
]) {
arr
.splice(i
, 1);
i
--;
}
}
}
console
.log(arr
);
</script
>
</body
>
</html
>
转载请注明原文地址:https://tech.qufami.com/read-234.html