js list 去重
时间: 2024-01-15 22:19:25 浏览: 77
以下是两种JavaScript去重的方法:
1. 使用Set数据结构
```javascript
var list = [1, 2, 1, {name: 1}, {name: 1}, 'Shaoyouqing', 'Shaoyouqing', null, null, NaN, 0, 0, {}, '', '', [1, 2], [1, 2], undefined, false, false, true, NaN, true, NaN, 9, undefined, 'true', 'false', 'true'];
var uniqueList = Array.from(new Set(list));
console.log(uniqueList);
```
2. 使用双重循环和splice方法
```javascript
var list = [1, 2, 1, {name: 1}, {name: 1}, 'Shaoyouqing', 'Shaoyouqing', null, null, NaN, 0, 0, {}, '', '', [1, 2], [1, 2], undefined, false, false, true, NaN, true, NaN, 9, undefined, 'true', 'false', 'true'];
for (var i = 0; i < list.length; i++) {
for (var j = i + 1; j < list.length; j++) {
if (list[i] === list[j]) {
list.splice(j, 1);
j--;
}
}
}
console.log(list);
```
阅读全文