JavaScript 数组去重
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 数组去重相关的知识,希望对你有一定的参考价值。
- 方法一
let a = [0, "a", "B", true, undefined, "", undefined, false, 0, null, 100, null, "a", "b", "", true];
console.log(a.toString());
a = fn(a);
console.log(a.toString());
function fn(a) {
if (a instanceof Array) {
let b = [];
a.forEach(item => {
if (!b.includes(item)) {
b.push(item);
}
});
return b;
} else {
return a;
}
}
- 方法二
let a = [0, "a", "B", true, undefined, "", undefined, false, 0, null, 100, null, "a", "b", "", true];
console.log(a.toString());
a = fn(a);
console.log(a.toString());
function fn(a) {
if (a instanceof Array) {
let b = [...new Set(a)];
return b;
} else {
return a;
}
}
以上是关于JavaScript 数组去重的主要内容,如果未能解决你的问题,请参考以下文章