Array.concat()返回typescript中的单独数组(Angular)

时间:2017-09-11 06:15:23

标签: javascript json angular typescript

我正在尝试使用复选框中的多个输入参数在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);
  } 
  }

1 个答案:

答案 0 :(得分:0)

很好地尝试了@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);
  }