In Python if you have a dictionary that consists of lists like
mydict = {'foo': [], 'bar':[3, 4]}
and if you want to add something into that lists you can do
mydict.setdefault('baz', []).append(5)
not to write
key, list_element = 'baz', 5
if key in mydict:
mydict[key].append(list_element)
else:
mydict[key] = [list_element]
is there an equivalent for this in Coffeescript?
i recommend against using ||/ or to test for membership—it's basically a clever trick that will silently fail against falsy values. i prefer to write
( mydict[ name ]?= [] ).push value
which i find clearer; it assumes that in case mydict does not have an entry for name or
that value is null or undefined, then an empty list should be put there at this point.
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