簡體   English   中英

Typescript函數轉換為Immutable.js Typescript列表

[英]Typescript function converted to Immutable.js Typescript List

我正在嘗試使用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]

哪一個讓我不滿意,似乎我沒有正確使用不可變列表方法。

在第二種情況下,您嘗試按索引刪除。 在第一種情況下,按價值計算。 這不一樣。 你需要換線

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

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

index重命名為value將減少混亂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM