# How to match two array using ngfor or ngif

**URL:** <https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792>\
**Category:** ionic-v3\
**Created:** [May 18, 2018, 12:23pm UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792 "2018-05-18T12:23:32Z")\
**Posts on this page:** 10\
**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:** [May 18, 2018, 12:23pm UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/1 "2018-05-18T12:23:32Z")

</div>

I have two array i want to do that if both array id is same the check box marked as a checked if not match its showing uncheck  
**Array1 response**

`[{"vdoSub_id":"1","vdoSub_name":"Art & Design","vdoSub_desc":"Here have some various art & design videos"},{"vdoSub_id":"2","vdoSub_name":"Comedy","vdoSub_desc":"Here we have some funny comedy videos. Please watch, like and share with your friends and family member."},{"vdoSub_id":"3","vdoSub_name":"Music","vdoSub_desc":"Here music destination featuring top tracks and popular hits from a variety of genres."},{"vdoSub_id":"4","vdoSub_name":"java","vdoSub_desc":"higjghjfhjfhhjfjhjghfjfgj"},{"vdoSub_id":"5","vdoSub_name":"XYZA","vdoSub_desc":"wgqghqwhgqer6yjke65533"}]`

**Array 2 response**  
`[{"subjectId":"2"},{"subjectId":"4"},{"subjectId":"1"}]`

I want to match **Array 2 subjectId == Array1 vdoSub\_id**

if match check box is checked as a marked if not checkbox in uncheck  
**below my html**

```auto
<ion-item *ngFor="let sub of allVideoSubject">
    <ion-label>{{sub.vdoSub_name}}</ion-label>
    <ion-checkbox [(ngModel)]="sub.vdoSub_id"></ion-checkbox>
  </ion-item>

```

please help me any one how can i do that  
any help appreciate.  
Thaks

---

<div class="post-metadata">

**Author:** ![umerf6455](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/umerf6455/32/147397_2.png) [@umerf6455](https://forum.ionicframework.com/u/umerf6455)\
**Post date:** [May 19, 2018, 10:52pm UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/2 "2018-05-19T22:52:12Z")

</div>

Hey @flycoders_sourav, you can compare and check the checkbox as follows:

```auto
     <ion-item *ngFor="let sub of allVideoSubject">
        <ion-label>{{sub.vdoSub_name}}</ion-label>
        <ion-checkbox *ngIf="Array2.subjectId== Array1.vdoSub_id" checked=true></ion-checkbox>
        <ion-checkbox *ngIf="Array2.subjectId!= Array1.vdoSub_id" checked=false></ion-checkbox>
      </ion-item>

```

---

<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:** [May 21, 2018, 4:39am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/3 "2018-05-21T04:39:08Z")

</div>

> [@umerf6455](#):
>
> \<ion-item \*ngFor=“let sub of allVideoSubject”\>  
> \<ion-label\>{{sub.vdoSub\_name}}\</ion-label\>  
> \<ion-checkbox \*ngIf=“Array2.subjectId== Array1.vdoSub\_id” checked=true\>\</ion-checkbox\>  
> \<ion-checkbox \*ngIf=“Array2.subjectId!= Array1.vdoSub\_id” checked=false\>\</ion-checkbox\>  
> \</ion-item\>

Showing an error array1 is undefine

```auto
below my code 

   ionViewDidLoad() {
     this.getAllsubject();
      this.storage.get('user_id').then((value) => {
        let userId=value;
   this.http.get(this.App.url+"sub/"+9)
      .subscribe(data => {
       let ServerResponse =data.json()
       let response = ServerResponse.code;

       this.subscriptionvideo=ServerResponse.subscriptionvideo; 
       this.all=ServerResponse.AllPaidSubject; 
       for(let i=0; i<this.all.length;i++){

         alert(this.all[0].subjectId);
       }
      }, error => {
        // Error getting the data
        console.log(error);
      });
      });

  }
getAllsubject(){

  this.http.get(this.App.url+"getAllSubject")
      .subscribe(data => {
        let serverResponse= data.json();
        let responseCode = serverResponse.code;
         let users_id= serverResponse.users_id;
         this.allVideoSubject = serverResponse.AllVideoSubject;
      }, error => {
        //console.log(error);// Error getting the data
       //this.presentToast("No Internet connection!")
      });
}

```

---

<div class="post-metadata">

**Author:** ![umerf6455](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/umerf6455/32/147397_2.png) [@umerf6455](https://forum.ionicframework.com/u/umerf6455)\
**Post date:** [May 21, 2018, 6:07am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/4 "2018-05-21T06:07:35Z")

</div>

Oh oo. Sorry Bro I user names Array1 and Array2 as just an example. You have to use your actual array names. Like in your case I think its all and allVideoSubject. Try using actual array names in which you are getting your data instead of Array1 and Array2. Let me know if you got the solution.

---

<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:** [May 21, 2018, 7:04am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/5 "2018-05-21T07:04:47Z")

</div>

i used my actual array name called. name of the array is **all**

---

<div class="post-metadata">

**Author:** ![umerf6455](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/umerf6455/32/147397_2.png) [@umerf6455](https://forum.ionicframework.com/u/umerf6455)\
**Post date:** [May 21, 2018, 7:30am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/6 "2018-05-21T07:30:08Z")

</div>

Can you show me your html code where you are looping through the arrays?

---

<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:** [May 21, 2018, 7:40am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/7 "2018-05-21T07:40:44Z")

</div>

```auto

 <ion-item *ngFor="let sub of allVideoSubject">
        <ion-label>{{sub.vdoSub_name}}</ion-label>
        <ion-checkbox *ngIf="all.subjectId== allVideoSubject.vdoSub_id" checked=true></ion-checkbox>
        <ion-checkbox *ngIf="all.subjectId!= allVideoSubject.vdoSub_id" checked=false></ion-checkbox>
      </ion-item>

```

---

<div class="post-metadata">

**Author:** ![umerf6455](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/umerf6455/32/147397_2.png) [@umerf6455](https://forum.ionicframework.com/u/umerf6455)\
**Post date:** [May 21, 2018, 8:05am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/8 "2018-05-21T08:05:26Z")

</div>

The code where you are using \*ngIf, you are comparing all.subjectId== allVideoSubject.vdoSub\_id. Don’t you think you have to use it like this:  
all.subjectId== sub.vdoSub\_id

I mean instead of allVideoSubject.vdoSub\_id, you should use sub.vdoSub\_id

I hope this was the issue

---

<div class="post-metadata">

**Author:** ![damonsomu](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@damonsomu](https://forum.ionicframework.com/u/damonsomu)\
**Post date:** [July 11, 2018, 7:41am UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/9 "2018-07-11T07:41:21Z")

</div>

I don’t think all.subjectId== sub.vdoSub\_id will solve the issue. I also have the same kind of case but not solved yet. @flycoders_sourav did you succeed to solve this???

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:35pm UTC](https://forum.ionicframework.com/t/how-to-match-two-array-using-ngfor-or-ngif/130792/10 "2021-07-19T21:35:15Z")

</div>

Closed on July 19, 2021, 9:35pm UTC


