Skip to content Skip to sidebar Skip to footer

How Do I Remove JQuery With The Same Value?

My question is very simple, but I could not. JSON has a data. I'm doing push with loop somewhere. But the same values ​​are added again. I want to solve this. There will be onl

Solution 1:

Here is a try using filter and findIndex

  thelist = [{"name": "test"}, {"name": "test2"}, {"name": "test"}]

  Array.prototype.distinctBy= function(key){
     return this.filter((value, index, self)=> {
      return self.findIndex((v, i)=> v[key] === value[key]) === index
     });
  
  }

  console.log(thelist.distinctBy("name"))

Solution 2:

Based on your image the object should look like this

thelist = [{"name": "test"}, {"name": "test2"}, {"name": "test"}]

and you can use filter to achieve your desired result

thelist = thelist.filter(
            (item, index, self) =>
                index ===
                self.findIndex(t =>
                    Object.keys(thelist[0]).every(prop => t[prop] === item[prop])
                )
        );

Post a Comment for "How Do I Remove JQuery With The Same Value?"