Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Ruby 中,我可以这样做:
hash = ['foo', 'bar'].each_with_object({}) { |i, h| h[i] = 0 }
我如何在 CoffeeScript 中做同样的事情,最好使用一些优雅的单线?
一种方法是这样的:
hash = {} hash[key] = 0 for key in ["foo", "bar"]
此外,在 Ruby 示例中,您可以使用each_with_object而不是inject这样您就不需要h在最后返回变量:
each_with_object
inject
h