Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImmutableJS - delete element from Map

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
like image 513
user3696212 Avatar asked Jun 30 '15 02:06

user3696212


2 Answers

Just use the delete method and the property in double quotes:

theFormerMap = theFormerMap.delete("2")
like image 198
Johann Echavarria Avatar answered Oct 26 '22 22:10

Johann Echavarria


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 {} }
like image 20
gabrielf Avatar answered Oct 26 '22 22:10

gabrielf