如何在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。
关于immutable.js - Immutable.js-是否可以将元素插入列表中的任意位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163457/