array.concat()返回打字稿中的单独数组(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);
  }

最新更新