原生JS 数组every方法去重
程序员文章站
2022-06-29 20:03:36
...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//every() 每一项都为满足条件 才返回true
//some() 只要有一个满足就返回true;
var arr = [1, 1, 2, 2, 3, 3, 2, 4, 1];
var newArr = [];
for (let i = 0; i < arr.length; i++) {
if (newArr.some(item => item == arr[i])) {
continue; // 继续循环
}
newArr.push(arr[i]);
}
console.log(newArr);
</script>
</body>
</html>
上一篇: 一.js数组之every()方法与some()方法
下一篇: 学习日志1