请老师检查
来源:3-11 项目作业
Cynthia4660559
2022-03-19 01:48:15
<!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>
const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}];
// solution1
const s = new Set(arr);
console.log([...s]);
// solution2
let newArr = arr.reduce(function (prev,next) {
prev.indexOf(next)==-1 && prev.push(next);
return prev;
},[])
console.log(newArr);
// solution3
let a = [];
for (let index = 0; index < arr.length; index++) {
if (a.indexOf(arr[index])==-1) {
a.push(arr[index]);
}
}
console.log(a);
// solution4
let m = new Map();
let b = [];
for (let index = 0; index < arr.length; index++) {
m.set(arr[index],null);
}
m.forEach(function (value,key) {
b.push(key);
})
console.log(b);
</script>
</body>
</html>1回答
好帮手慕星星
2022-03-19
同学你好,四种去重方式都是正确的,很棒!祝学习愉快~
相似问题