So I have an Immutable js map:
{
title: "item 1",
title2: "item 2",
title3: "item 3"
}
I want to update the keys, so it will become:
{
new title: "item 1",
title2: "item 2",
title3: "item 3"
}
so far with update()
and set()
I can only update the values? Is it possilbe to update the key or do I need to convert it before I can update the keys?
you can use mapKeys as :
var map = Immutable.Map({
new title: "item 1",
title2: "item 2",
title3: "item 3"
});
map = map.mapKeys(k => {
if (k === "title1")
return "newTitle";
return k;
});
another, more direct, approach is to use delete
var map = Immutable.Map({
title: "item 1",
title2: "item 2",
title3: "item 3"
});
map = map.set('new title', "item 1")
.delete('title');
or
map = map.set('new title', map.get('title'))
.delete('title');
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