判断list数组里的json对象有无重复,有则去重留1个

时间:2023-03-09 18:42:18
判断list数组里的json对象有无重复,有则去重留1个

查找有无重复的

var personLength = [{ certType: '2015-10-12', certCode:'Apple'}, { certType: '2015-10-12', certCode:'Apple'}, { certType: '2015-10-13', certCode :'Apple' }]
var find = false;
for(var i = 0; i < personLength.length; i++){
for(var j = i + 1; j < personLength.length; j++){
if(personLength[i].certType==personLength[j].certType&& personLength[i].certCode==personLength[j].certCode){
find = true;
var flagIndex = j;
break;
}
}
if (find) break;
}

去重(去掉重复的,保留一个)

let person= [{ certType: '2015-10-12', certCode:'Apple'}, { certType: '2015-10-12', certCode:'Apple'}, { certType: '2015-10-13', certCode :'Apple' }]
let obj = {};
person = person.reduce((cur,next) => {
obj[next.certType+next.certCode] ? "" : obj[next.certType+next.certCode] = true && cur.push(next);
return cur;
},[])
console.log(person)//这是去掉重复后的数据