测试:数据
let arr = [
{ a: 1, b: 2 },
{ b: 3, a: 4 },
{
b: 2, a: 1
}
]
结果
function unique(arr) {
const newArr = [...arr]
for (let i = 0; i < newArr.length; i++) {
for (let j = i + 1; j < newArr.length; j++) {
if (this.duibi(newArr[i], newArr[j])) {
newArr.splice(j, 1);
j--;
}
}
}
return newArr;
// return Array.from(new Set(arr));
}
function duibi(val1, val2) {
if (isObject(val1) && isObject(val2)) {
if (val1.length != val2.length) {
return false;
}
let key1 = Object.keys(val1);
let key2 = Object.keys(val2);
for (let k1 of key1) {
if (!key2.includes(k1)) {
return false;
}
if (!duibi(val1[k1], val2[k1])) {
return false;
}
}
return true;
} else {
return val1 == val2
}
}
//是否为集合
function isObject(obj) {
return obj !== null && typeof obj === 'object';
}