How to loop two array and compare it if same checkbox is checked Angular 2 -


i have 1 array contain office list. array user selected list. want display officelist , if value in selected list same office list checkbox checked. how did it.
code

<div *ngfor="let item of officelist">   <div *ngfor="let officedata of alloffice.office">     <div *ngif="item.office_id == officedata.office_id">       <input #officearray type="checkbox" id="officearray" name="officearray" class="form-control" value="{{item.officename}}"         checked> {{(item.officename == "" ? "--no data--" : item.officename)}}     </div>     <div *ngif="item.office_id != officedata.office_id">       <input #officearray type="checkbox" id="officearray" name="officearray" class="form-control" value="{{item.officename}}">      {{(item.officename == "" ? "--no data--" : item.officename)}}     </div>   </div> </div> 

and result isenter image description here

my officelist

this.officelist = [   { "office_id": "1", "officename": "sun dept" },   { "office_id": "2", "officename": "moon" },   { "office_id": "3", "officename": "stars" } ] 

alloffice.office array

"office": [     {       "office_id": "1",       "officename": "sun dept"     },     {       "office_id": "2",       "officename": "moon dept"     }   ] 

use solution.this works fine me. have done things in constructor .if want in method use code block inside constructor.

this ts file:

  officelist: array<any> = [     { "office_id": "1", "officename": "sun dept" },     { "office_id": "2", "officename": "moon" },     { "office_id": "3", "officename": "stars" }   ];     office: array<any> = [     {       "office_id": "1",       "officename": "sun dept"     },     {       "office_id": "2",       "officename": "moon dept"     }   ];   newarray: array<any> = [];   constructor() {     (var = 0; < this.officelist.length; i++) {        var ismatch = false; // haven't found yet        (var j = 0; j < this.office.length; j++) {          if (this.officelist[i].office_id == this.office[j].office_id) {           // have found this.officelist[i]] in this.office, can stop searching           ismatch = true;           this.officelist[i].checked = true;//  checkbox status true           this.newarray.push(this.officelist[i]);           break;         }//end if         // if never find this.officelist[i].office_id in this.office, loop end,         // , ismatch remain false       }       // add this.officelist[i] newarray if didn't find match.       if (!ismatch) {         this.officelist[i].checked = false;//  checkbox status false         this.newarray.push(this.officelist[i]);       } //end if     }     console.log(this.newarray);    } 

this html:

<div *ngfor="let item of newarray">   <input type="checkbox" [checked]="item.checked"> {{item.officename}} </div> 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -