2016-12-13 6 views
2

나는 내가 타이프 라이터 기능은

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); 
     } 
    } 

이로 변환하려고이 내 수업에서

Immutable.js 목록 방법을 사용하여 새 기능 내 기능을 변환하기 위해 노력하고있어 Immutable.js 타이프 라이터 목록으로 전환 :

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

두 번째 경우 인덱스로 삭제하려고합니다. 첫 번째 경우에는 값으로. 이것은 동일하지 않습니다. 당신은 혼란을 줄일 수 indexvalue에를 Renameing

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

에 선

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

을 변경해야합니다.