title: How to mark as selected rows in an angular material table when object exists in another array
body: <p>I want to mark the rows of the table as selected if they are in a different array then the tables dataSource. </p>
  
  <p>This is what I've done so far:</p>
  
  <p>component.ts</p>
  
  <pre><code>dataSource = new MatTableDataSource&lt;MyModel&gt;([]);
  selection = new SelectionModel&lt;MyModel&gt;(true, []);
  arrayToCompareWith: AnotherModel[];
  
  //The part use for selection in UI copy from Angular Material Docs
  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.resultsDs.data.length;
    return numSelected === numRows;
  }
  
  masterToggle() {
    this.isAllSelected() ?
    this.selection.clear() :
    this.resultsDs.data.forEach(row =&gt; this.selection.select(row));
  }
  
  checkboxLabel(row?: SdsSectionModel): string {
    if (!row) {
      return `${this.isAllSelected() ? 'select' : 'deselect'} all`;
    }
    return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${row.sectionId + 1}`;
  }
  
  \\The method I'm trying to use to select the rows in code
  addToSelectedRows(): void{
    for(let option of this.arrayToCompareWith){
       \\myModelProp is of type MyModel
       this.selection.toggle(option.myModelProp);
    }
  }
  </code></pre>
  
  <p>This actually selects the objects and adds them to selection object but it does not update the UI.</p>
  
  <p>and this is the html for the selection column it was taken from the angular material docs also:</p>
  
  <pre><code>&lt;ng-container matColumnDef="select"&gt;
     &lt;th mat-header-cell *matHeaderCellDef&gt;
        &lt;mat-checkbox (change)="$event ? masterToggle() : null"
                      [checked]="selection.hasValue() &amp;&amp; isAllSelected()"
                      [indeterminate]="selection.hasValue() &amp;&amp; !isAllSelected()"
                      [aria-label]="checkboxLabel()"&gt;
        &lt;/mat-checkbox&gt;
     &lt;/th&gt;
     &lt;td mat-cell *matCellDef="let row"&gt;
        &lt;div style="width: 3px"&gt;
          &lt;mat-checkbox (click)="$event.stopPropagation()"
                        (change)="$event ? selection.toggle(row) : null"
                        [checked]="selection.isSelected(row)"
                        [aria-label]="checkboxLabel(row)"&gt;
          &lt;/mat-checkbox&gt;
        &lt;/div&gt;
     &lt;/td&gt;
  &lt;/ng-container&gt;
  </code></pre>