工作中遇到的重复问题
从接口拿到判断重复的字段,比如:电话,姓名等,根据这些字段判断数组中有没有重复
const array = [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
{ id: 1, name: "John" },
{ id: 1, name: "Jim" },
];
// 判断是否有重复
function getIsUniqueArray(array, repeatKeys) {
const isUnique = array.some((obj, index, self) =>
index !==
self.findLastIndex((t) => {
return repeatKeys.every((key) => {
return t[key] === obj[key];
});
})
);
return isUnique;
}
const isUniqueArray = getIsUniqueArray(array, ["id", "name"]);
console.log(isUniqueArray);
// 判断是否有重复
function getIsRepeatArray(arr, repeatKeys) {
const map = {}
return arr.some(item => {
const key = repeatKeys.map(key => item[key]).join('-')
if (map[key]) {
return true
} else {
map[key] = true
return false
}
})
}
const isRepeatArray = getIsRepeatArray(array, ["id", "name"]);
console.log(isRepeatArray, 'isRepeatArray');
过滤掉去重之后的数组
// 去重之后的数组
function getUniqueArray(array, repeatKeys) {
const unique = array.filter((obj, index, self) =>
index ===
self.findIndex((t) => {
return repeatKeys.every((key) => {
return t[key] === obj[key];
});
})
);
return unique;
}