I want to swap items inside an immutable list within a Map
,
example:
const Map = Immutable.fromJS({
name:'lolo',
ids:[3,4,5]
});
i am trying to use splice to do the swapping, also tried with insert()
an Immutable method.
Lets say i want to swap from [3, 4, 5]
to [3, 5, 4]
, i am truing something like this:
list.set('ids', list.get('ids').splice(2, 0, list.get('ids').splice(1, 1)[0])
What's the best way to sort elements inside an Immutable data structures with Immutable.js ?
You can use destructuring assignment to swap values at different indexes of array
plnkr http://plnkr.co/edit/ZcEFNIFTnji3xxs7uTCT?p=preview
Parametrized:
You should use
Map.update()
andList.withMutations()
for that matter:Notice that I renamed your
list
tomap
- this is actually aMap
.And for simple sorting go for