标题 在js中有一个数组 数组里的每一项有phone字段 如何判断数组里是否存在重复的phone
let arr = [
{ phone: '1234567890' },
{ phone: '9876543210' },
{ phone: '1234567890' },
{ phone: '1357924680' }
];
// 使用some方法判断是否存在重复的phone字段
function hasDuplicate(arr) {
return arr.some((item, index) => arr.findIndex((i) => i.phone === item.phone) !== index);
}
if (hasDuplicate(arr)) {
console.log('数组中存在重复的phone字段');
} else {
console.log('数组中不存在重复的phone字段');
}