In CoffeeScript sometimes I need to call Array.reduce(...)
with a default value; however, the unfortunate ordering of the arguments means the initial/default value goes after the reduce function itself, which means I've got to use a lot of parens, which seems much uglier than CoffeeScript wants to be.
For example:
items = [ (id:'id1', name:'Foo'), (id:'id2', name:'Bar') ] # ...
itemsById = items.reduce(((memo, item) -> # <-- Too many parens!
memo[item.id] = item
memo), {}) # Ugly!
Is there a more idiomatic way to do this in CS?
This works:
itemsById = items.reduce (memo, item) ->
memo[item.id] = item
memo
, {}
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