简体   繁体   English

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

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

I am trying to apply a filter on a JSON array using multiple input parameters from a checkbox and concatenate the results into a single array. 我正在尝试使用复选框中的多个输入参数对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);
    })
  }

The code prints multiple array declarations on screen, which would have been fine if every consecutive declaration had the value of previous concatenated into it. 该代码在屏幕上打印多个数组声明,如果每个连续的声明都将先前的值串联在一起,那就很好了。

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

I have fetched data into the array by use of a service: 我已经通过使用服务将数据提取到数组中:

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

then only 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 the suggestion provided by @Satpal and modified the code a bit inside the function. 很好地尝试了@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