2016-12-13 33 views
2

我试图使用Immutable.js列出方法打字稿功能转化为Immutable.js打字稿列表

在我的课,我有

public checkVisible:any = []; 

public rowChecked(index){ 
     if (this.checkVisible.indexOf(index) === -1) { 
      this.checkVisible.push(index); 
     } 
     else { 
      this.checkVisible.splice(this.checkVisible.indexOf(index), 1); 
      console.log(this.checkVisible); 
     } 
    } 

尝试转换到本我的功能转换为新功能:

public checkVisible:Immutable.List<any> = Immutable.List([]); 

public rowChecked(index){ 
    if (this.checkVisible.indexOf(index) === -1) { 
     this.checkVisible = this.checkVisible.push(index); 
    } 
    else { 
     this.checkVisible = this.checkVisible.delete(index); 
     console.log(this.checkVisible, 'REMOVED'); 
    } 
} 

第一函数的工作需要:

this.rowChecked(0); 
this.rowChecked(2); 
this.rowChecked(2); 
this.rowChecked now is Array[1] = [0:0] 

但不可变列表功能做到这一点:

this.rowChecked(0); 
this.rowChecked(2); 
this.rowChecked(2); 
this.rowChecked now is Array[2] = [0:0, 1:2] 

不满足我看来,我不恰当地使用不可变列表的方法。

回答

0

在第二种情况下,您尝试按索引删除。在第一种情况下,按价值。这是不一样的。您需要更改线路

this.checkVisible = this.checkVisible.delete(index); 

this.checkVisible = this.checkVisible.delete(this.checkVisible.indexOf(index)) 

Renameing indexvalue会减少混乱。