CoffeeScript 文档指出列表推导式应该能够执行选择/过滤操作:
They should be able to handle most places where you otherwise would use a loop, each/forEach, map, or select/filter.
你会想象你可以在一行中做一些事情,比如
result = item for item in list if item % 2 == 0
然而,我能来的最接近的是list = [1,2,3,4]
result = []
for item in list
if item % 2 == 0 then result.push item
在 CoffeeScript 中过滤列表的最简洁方法是什么?
最佳答案
result = (item for item in list when item % 2 == 0)
编辑 : 添加
result =
关于list-comprehension - 使用 CoffeeScript 列表推导式过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839818/