javascript - Array.concat() returns separate arrays in typescript(Angular) -


i trying apply filter on json array using multiple input parameters checkbox , concatenate results single array.

stats = {     : {value: 'all', prop: 'all', checked: true, disabled: false},     open : {value: 'open', prop: 'open', checked: false, disabled: false},      registered: {value: 'registered', prop: 'in progress', checked: false,                   disabled: false},      admitted: {value: 'admitted', prop: 'student admitted', checked: false,                     disabled: false},      inactive: {value: 'inactive', prop: 'converted', checked: false,                     disabled: false},          };      check: boolean = true; disable: boolean = true;     checkedstatus =[];     filtered = [];   //inside function//  if(checkerobj.checked === true){     this.checkedstatus.push(checkerobj.prop);     this.checkedstatus.foreach(el => {      var temp = [];      let temp2 = [];      temp = this.rows.filter(row => {       if(row.statusvalue === el){         temp = [];         //console.log(typeof row);         return row;       }       });       //console.log(temp);       var arr3 = temp2.concat(temp);      //this.source = new localdatasource(temp2);      console.log(arr3);     })   } 

the code prints multiple array declarations on screen, have been fine if every consecutive declaration had value of previous concatenated it.

enquiry-manage.component.ts:131  (17) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…},        {…}, {…}, {…}] enquiry-manage.component.ts:131   (6) [{…}, {…}, {…}, {…}, {…}, {…}] enquiry-manage.component.ts:131   (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 

i have fetched data array use of service:

this.enquire.getallenquiry().map(data => {   this.rows = data;  }).subscribe(data => {   this.source = new localdatasource(this.rows);   this.source.refresh(); }) 

then checkbox check event

  statusfilter(checkerobj){    if(this.stats.open.checked === true || this.stats.registered.checked ===     true || this.stats.admitted.checked === true ||     this.stats.inactive.checked === true){    this.stats.all.checked = false;    this.stats.all.disabled = true;        if(checkerobj.checked === true){     this.checkedstatus.push(checkerobj.prop);     this.checkedstatus.foreach(el => {      var temp = [];      let temp2 = [];      temp = this.rows.filter(row => {       if(row.statusvalue === el){         temp = [];         return row;       }       });       //console.log(temp);       temp2 = temp.concat(temp2);      //this.source = new localdatasource(temp2);      console.log(temp);     })   }   else if(checkerobj.checked === false){     var index = this.checkedstatus.indexof(checkerobj.prop);     if (index > -1){       this.checkedstatus.splice(index, 1);     }     this.checkedstatus.foreach(el => {     //          this.customfilterstatus(el);     })     //      console.log(this.checkedstatus);   }    } 

well tried suggestion provided @satpal , modified code bit inside function.

    if(checkerobj.checked === true){     this.checkedstatus.push(checkerobj.prop);     let temp2 = [];      let temp = [];     let arr = [];     this.checkedstatus.foreach(el => {        temp2 = this.rows.filter(row => {          return row.statusvalue === el });          temp = temp2;         arr = arr.concat(temp);       });       this.source = new localdatasource(arr);       console.log(arr);   } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -