+ 1
How to remove duplicates in JavaScript objects?
I have an array of objects, but I want to remove objects in that have the same properties like name, Address and email. I want to base on these three to remove the duplicate objects
3 Réponses
+ 1
That just the bare ideal
You can develop that item.email in map depend on  your "same":
// define how is the same here, ex: same name and email
Object.prototype.isSame = function (obj){
   return this.email && obj.email && this.email == obj.email && this.name && obj.name && this.name == obj.name 
}
// check had any same item in array
Object.prototype.haveSameIn = function (arr){
    for(var i= 0; i< arr.length;i++){
        if (this.isSame(arr[i]))
            return true 
    }
    return false
}
ar = [{email:'a', name: 'aa'}, {email:'b', name:'bb'}, {email:'a', name: 'aa'} ]
map = []
// similar W1
br = ar.filter(function(it){
   if (it.isHaveSameIn(map)){
      return false  
   }else{
      map.push(it)
      return true
   }
})
// Or use pop push
while (ar.length >0){
    it = ar.pop()
    if (!it.haveSameIn(map)){
        map.push(it)
    }
}
// Both have same result
console.log(map)
0
Way1:
var ar = [
{email:'a'},{email:'a'}, {email:'b'}
]
var map = {}
br = ar.filter(function (item){
   if (!(item.email in map) ){
      map[item.email] = item
      return true
   }else{
      return false 
   }
})
console.log(br)
W2:
// reduce to Object to remove duplicate
cr = ar.reduce(function(a,b){
      a[b.email]=b
    return a
}, {})
// Convert to array
cr = Object.keys(cr).map(function(i){
return cr[i]
})
console.log(cr)
0
I see u are only dealing with one object property. I meant something like this
 [{name:'chad', address:street,tel:'0764645455'},{name:'chad', address:street,tel:'0764645455}]





