如何在Immutable.js列表的任意位置插入元素?
请您参考如下方法:
您正在寻找 splice method:
Splice returns a new indexed Iterable by replacing a region of this Iterable with new values.
splice(index: number, removeNum: number, ...values: any[])
您可以在其中指定
index的位置,如果您将
0编写为
removeNum,它将仅在指定位置插入值:
var list = Immutable.List([1,2,3,4]);
console.log(list.toJS()); //[1, 2, 3, 4]
var inserted = list.splice(2,0,100);
console.log(list.toJS()); //[1, 2, 3, 4]
console.log(inserted.toJS()); //[1, 2, 100, 3, 4]
演示
JSFiddle。


