[英]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.