我正在尝试使用 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]
我不满意,似乎我没有正确使用不可变列表方法。
最佳答案
在第二种情况下,您尝试按索引删除。在第一种情况下按值(value)。这是不一样的。你需要换行
this.checkVisible = this.checkVisible.delete(index);
到
this.checkVisible = this.checkVisible.delete(this.checkVisible.indexOf(index))
将 index
重命名为 value
将减少混淆。
关于javascript - Typescript 函数转换为 Immutable.js Typescript 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41119562/