相关疑难解决方法(0)

Immutable JS OrderedMap: Insert a new entry after a given Key

I have an Immutable OrderedMap as follows:

pairs: Immutable.OrderedMap({"Key1":"Value1","Key2":"Value2","Key4":"Value4"})
Run Code Online (Sandbox Code Playgroud)

I need to insert ["Key3":"Value3"] after ["Key2":"Value2"] dynamically.

I thought

pairs.MergeIn(['Key2'],OrderedMap({"Key3":"Value3"}))  
Run Code Online (Sandbox Code Playgroud)

will serve the purpose but not working.

I tried

const newPairs=Immutable.OrderedMap();
newPairs.forEach(function(value,key,map)){
   if(key=="Key4")
     newPairs.set('Key3','Value3')
   newPairs.set(key,value')
});     
Run Code Online (Sandbox Code Playgroud)

But I know it's a stupid code which won't work as newPairs is immutable and newPairs will be still empty. So is there any Immutable way of OrderedMap.addBefore(Key,key,value)?

javascript ordered-map immutable.js

1
推荐指数
1
解决办法
2856
查看次数

标签 统计

immutable.js ×1

javascript ×1

ordered-map ×1