Delete Duplicate Objects

remove duplicate objects from an array
 avatar
Pranav
javascript
2 years ago
656 B
2
Indexable
Never
    
    
    let array =
    
        [
    
        {id:"11",name:"sample1"},
     
        {id:"12",name:"sample2"},
     
        {id:"11",name:"sample3"},
     
        {id:"13",name:"sample4"},
     
        {id:"11",name:"sample5"}
        
        ]
    
    let del_dup = {}
    
    array.map((arr)=>{
        
       const { id, name } = arr
        
        if(del_dup[id])
        {
            
        del_dup[id].times = del_dup[id].times+1
        
        }
    
    else
    {
        del_dup[id] = 
        {
            id:arr.id,
            name:arr.name,
            times:1
        }
    }
    
    })
    
    console.log(del_dup)