How to remove duplicate value from an array

actually i want to checking at the time of pushing the value into the array. If duplicate then the value will not push.how can i do that.

public selectcity:any=[];
public test: any = [];
i want to push the whole value into the test array. when pushing the value from selectcity array at the time. i want to check If duplicate then the value will not push.how can i do that.

below my array push code:

  for(let i =0;i<this.selectcity.length;i++){
      	
            this.test.push(this.selectcity[i]);
  }

please help me any one

Lodash has a function that does exactly that.

how to implement the function please give me a sample of code.

I’m done with you. You might be the laziest leech I’ve ever seen in years of support forums.

1 Like

You can use hasOwnProperty on your test array in the loop to check if it already contains that element.

arr = [1,2,3,4,5,6,66,77];
arr.hasOwnProperty(66); // Returns true, because it has this vaue

This way you can skip one step in your loop.

But your array contains list of objects.
You have to find your way as @rapropos mentioned.
Search through internet, this one is easy to find out.

I use Set.add() and then Array.from()

Actually i want to do .prevent adding duplicate values to a array.

I’m using this below code.

   for(let i =0;i<this.mydata.length;i++){
        if(this.brandName.indexOf(this.brandName) == -1) {
           this.brandName.push(this.mydata[i]);
           }    
        }

please help anyone.