Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1dc2a8

Browse files
committedJul 15, 2020
数字数组和对象数组去重
1 parent eb361a3 commit e1dc2a8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<script>
10+
// 数组去重,数字或者对象数组去重
11+
// 对象id标识唯一性
12+
var arr1 = [1, 3, 3, 2, 0, 1];
13+
var arr2 = [
14+
{
15+
id: 0,
16+
a: 1,
17+
b: 2,
18+
},
19+
{
20+
id: 0,
21+
a: 1,
22+
b: 3,
23+
},
24+
{
25+
id: 1,
26+
a: 1,
27+
b: 2,
28+
},
29+
];
30+
let unique1 = (arr) => {
31+
return [...new Set(arr)];
32+
};
33+
console.log(unique1(arr1));
34+
35+
let unique2 = (arr) => {
36+
var hash = [];
37+
var result = [];
38+
arr.forEach((item, i) => {
39+
if (!hash[item.id]) {
40+
result.push(item);
41+
hash[item.id] = 1;
42+
}
43+
});
44+
return result;
45+
};
46+
console.log(unique2(arr2));
47+
</script>
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.