Remove duplicate objects from an array using javascript

后端 未结 9 1889
野性不改
野性不改 2020-12-02 21:21

I am trying to figure out an efficient way to remove objects that are duplicates from an array and looking for the most efficient answer. I looked around the internet everyt

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 21:48

    Below code compares object with JSON as String format and removes duplicates and works fine with simple arrays.

        Array.prototype.unique=function(a){
         return function(){
            return this.filter(a)
         }
       }(
       function(a,b,c){
         var tmp=[]; 
         c.forEach(function(el){
            tmp.push(JSON.stringify(el))
        }); 
        return tmp.indexOf(JSON.stringify(a),b+1)<0
      })
    

提交回复
热议问题