# How to remove duplicate value from an array

**URL:** <https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545>\
**Category:** ionic-v3\
**Created:** [August 29, 2017, 7:36am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545 "2017-08-29T07:36:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [August 29, 2017, 7:36am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/1 "2017-08-29T07:36:00Z")

</div>

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:**

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

```

please help me any one

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 29, 2017, 7:41am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/2 "2017-08-29T07:41:40Z")

</div>

Lodash has a function that does exactly that.

---

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [August 29, 2017, 7:49am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/3 "2017-08-29T07:49:24Z")

</div>

> [@rapropos](#):
>
> Lodash has a function that does exactly that.

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

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 29, 2017, 7:53am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/4 "2017-08-29T07:53:09Z")

</div>

> [@flycoders\_sourav](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![sujit77](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@sujit77](https://forum.ionicframework.com/u/sujit77)\
**Post date:** [August 29, 2017, 8:08am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/5 "2017-08-29T08:08:13Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 29, 2017, 8:13am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/6 "2017-08-29T08:13:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [August 31, 2017, 4:27am UTC](https://forum.ionicframework.com/t/how-to-remove-duplicate-value-from-an-array/103545/7 "2017-08-31T04:27:15Z")

</div>

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

**I’m using this below code.**

```auto
   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.
