2014-11-16 49 views
0

有沒有辦法如何格式化多個內部for-in循環以寫入更多行?在Coffeescript中格式化多重for-in循環

例子:

numbers = [1..20] 
addLetters= (number) -> 
    pairs = [] 
    pairs.push number+"A" 
    pairs.push number+"B" 
    pairs 

console.log pair for pair in addLetters number for number in numbers 

所需的格式,是這樣的:

# NOT WORKING 
console.log pair 
    for pair in addLetters number 
     for number in numbers 

JSFiddle

回答

0

你的 「不工作」 環路內而外。外環先走:

for number in numbers 
    for pair in addLetters number 
     console.log pair 

此外,您的addLetters功能過於複雜。

addLetters = (number) -> 
    [ "#{number}A", "#{number}B" ] 

演示:http://jsfiddle.net/ambiguous/b9p1v87m/

你可以用一個數組文本構建返回數組