簡體   English   中英

Array.concat()以typescript(Angular)返回單獨的數組

[英]Array.concat() returns separate arrays in typescript(Angular)

我正在嘗試使用復選框中的多個輸入參數對JSON數組應用過濾器,並將結果串聯到單個數組中。

stats = {
    All : {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);
    })
  }

該代碼在屏幕上打印多個數組聲明,如果每個連續的聲明都將先前的值串聯在一起,那就很好了。

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

我已經通過使用服務將數據提取到數組中:

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

然后只有復選框檢查事件

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

很好地嘗試了@Satpal提供的建議,並在函數內部進行了一些修改。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM