【问题标题】:Array.concat() returns separate arrays in typescript(Angular)Array.concat() 在 typescript(Angular) 中返回单独的数组
【发布时间】:2017-09-11 06:15:22
【问题描述】:

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

【问题讨论】:

  • Array.concat() 总是返回一个新数组。什么问题?可能你需要if (checkerObj.checked === true) { this.checkedStatus.push(checkerObj.prop); let temp2 = this.rows; this.checkedStatus.forEach(el => { temp2 = temp2.filter(row => { return row.statusValue === el }); }) console.log(temp2); }
  • @Satpal 实际上我希望将传入的数组合并到前一个数组中。还尝试了 [].push.apply() 但输出相同。有什么方法可以将我作为输出得到的这三个数组合并为一个
  • @Satpal 如果我提供单个输入,上面的代码将是完美的,但如果我过滤已经过滤的行,那么在第二个输入上将打印出一个空数组。
  • 请分享完整的代码,重要的一行是let temp2 = this.rows;请在正确的位置
  • 更像是在表格上,我通过复选框提供输入以过滤特定列,并且在多项选择中,我将获得一个数组作为所有这些参数的输出。

标签: javascript json angular typescript


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2021-11-26
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多