Javascript Typescript函数已转换为Immutable.js Typescript列表
我正在尝试使用Immutable.js列表方法将我的函数转换为新函数 我在班上有Javascript Typescript函数已转换为Immutable.js Typescript列表,javascript,typescript,immutable.js,Javascript,Typescript,Immutable.js,我正在尝试使用Immutable.js列表方法将我的函数转换为新函数 我在班上有 public checkVisible:any = []; public rowChecked(index){ if (this.checkVisible.indexOf(index) === -1) { this.checkVisible.push(index); } else { this.checkVisible
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');
}
}
但immutable list函数可以执行以下操作:
this.rowChecked(0);
this.rowChecked(2);
this.rowChecked(2);
this.rowChecked now is Array[2] = [0:0, 1:2]
这不符合我的要求,似乎我没有正确使用不可变列表方法。在第二种情况下,您尝试按索引删除。在第一种情况下,使用值。这是不一样的。你需要换线 到 将
索引
重命名为值
将减少混淆
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))