Add a key to map if it does not exist

I have a variable map_1, I don’t know if it has the key x, I want to add x: 4 only if the field does not exist in map_1 or else I don’t want to replace it

Map.put_new(my_map, :key, value) will only put the key/value if the key does not exist in the map.

5 Likes