Is there a nice way to get hash as a loop result in coffeescript? -
There are expressions in coffee loops and give an array. For example, we may
func = (name) -> can do. Propagate to take support in name.toUpperCase () Results = (['A', 'B']) and get as a result: < pre> ['a', 'b'] But if I want to get the hash table instead of the array? {a: 'A', b: 'B'} ? How to modify the example given above: I know, I can make an ID like this:
func = (name) - & gt; Name.toUpperCase () Result = {} result [prop] = ['A', 'B'] Function for Process for Prok Very literal! What could be a good way?
is not as good as understanding the real object, you can use ara :: Reduce for this. Specifically for your example, using the "transformer" function such as: transform = (f) -> (Previous, Carru) - & gt; previous [curr] = f curr prev and your func : func = (name) -> name.toUpperCase () You can do the same to your array like transform and func : result = ['a', 'b']. Reduce the change (func), {} See a longer description and more examples.
Comments
Post a Comment