I have a map with this structure:
{
1: {},
2: {}
}
And I'd like to delete 2: {} from it (of course - return new collection without this). How can I do it? I tried this, but something is wrong:
theFormerMap.deleteIn([],2) //[] should mean that it's right in the root of the map, and 2 is the name of the object I want to get rid of
Just use the delete method and the property in double quotes:
theFormerMap = theFormerMap.delete("2")
Just use the delete method and pass the property you want to delete:
theFormerMap = theFormerMap.delete(2)
If this does not work then you have probably created theFormerMap
using fromJS
:
Immutable.fromJS({1: {}, 2: {}}).delete(2)
=> Map { "1": Map {}, "2": Map {} }
Key 2 is not removed as it is, in fact, a string key. The reason is that javascript objects convert numeric keys to strings.
However Immutable.js does support maps with integer keys if you construct them without using fromJS
:
Immutable.Map().set(1, Immutable.Map()).set(2, Immutable.Map()).delete(2)
=> Map { 1: Map {} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With