0

How to compare 2 array of objects and when value is matched then checked angular material checkbox? eg: In this case operator object is matched so checkbox is checked for operator

app.ts

const arr1 = [ 
   { 
      "id":"1",
      "name":"operator"
   },
   { 
      "id":"2",
      "name":"admins"
   }
]
const arr2 = [ 
   { 
      "id":"1",
      "name":"operator"
   },
   { 
      "id":"3",
      "name":"client"
   }
]

this.arr1.forEach(a1Obj => {
    this.arr2.some(a2Obj => {
        if (a2Obj.id === a1Obj.id) {
            console.log(a1Obj);
        }
    })
});

app.html

<div class="col" *ngFor="let group of groups; let i = index">
    <mat-checkbox value="group.id" [labelPosition]="'after'" [checked]="true" (change)="assignGroups($event, group.id)">
        {{ group.name }}
    </mat-checkbox>
</div>
SKL
  • 1,243
  • 4
  • 32
  • 53

1 Answers1

1

Try like below,

Considering your arrays as array1 and array2,

this.array1.forEach(a1Obj => {
    this.array2.some(a2Obj => {
        if (a2Obj.id === a1Obj.id) {
            console.log(a1Obj); // this object is there in array2 as well.
        }
    })
});
Gangadhar Gandi
  • 2,162
  • 12
  • 19