How to match two array using ngfor or ngif


#1

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

<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


#2

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

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


#3

Showing an error array1 is undefine

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!")
      });
}

#4

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.


#5

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


#6

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


#7

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

#8

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


#9

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???