简体   繁体   English

Typescript函数转换为Immutable.js Typescript列表

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

I'm trying to convert my function to new function using Immutable.js List Methods 我正在尝试使用Immutable.js列表方法将函数转换为新函数

In my class i have 在我班上

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

Trying to convert to this : 尝试转换为此:

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

first function works as needed : 第一个功能根据需要工作:

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

But the immutable list function do this: 但是不可变列表功能可以做到这一点:

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

Which does not satisfy me it seems I do not use the Immutable List Methods properly. 哪一个让我不满意,似乎我没有正确使用不可变列表方法。

In the second case, you try to delete by index. 在第二种情况下,您尝试按索引删除。 In the first case by value. 在第一种情况下,按价值计算。 This is not the same. 这不一样。 You need to change the line 你需要换线

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

to

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

Renameing index to value will reduce confusion. index重命名为value将减少混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM