let arr1 = [{
name: "wzf",
sex: "nan",
width: "00000",
},
{
name: "wzf",
sex: "nan",
width: "gfd",
},
{
name: "wzf",
sex: "3234",
width: "34567",
},
{
name: "hgfdgnm",
sex: "narryttyn",
width: "34567",
}
];
//對象型數組去重
function deWeight(arr, ...arguements) {
// console.log(arguements);//獲得需要去重的字段數組
function getBoolean(o, m) {
let list = arguements.map(x => o[x] == m[x]); //值均為布爾
return list.every(i => i); //要使這些布爾值都真才能滿足條件,因為要求的條件是 並且
}
let result = [];//新數組
//總數組與新數組比較,遍歷總數組時用新數組的some方法進行判斷
arr.map(o => !result.some(m => getBoolean(o, m)) ? result.push(o) : '');
return result;
}
let resultArr = deWeight(arr1, 'name', 'sex'); //指定需要按哪些字段去重,支持按多個字段去重
console.log(resultArr);