问题描述
所以我试图让一个多维数组中的CoffeeScript工作。我曾尝试与标准的Python列表COM prehension符号,这使得内部支架的字符串或东西。所以我不能做列表[0] [1]拿到1,我不是获取列表[0] [0] ='1,1'和列表[0] [1] =''
So I'm trying to get a multidimensional array to work in CoffeeScript. I have tried with standard Python list comprehension notation, that makes the inner bracket a string or something. so I can't do list[0][1] to get 1, I instead get list[0][0] = '1,1' and list[0][1] = ''
[[i, 1] for i in [1]]
使用类作为储存容器,以然后抓住x和y。这使不确定不确定,而不是1的后半部分。
Using a class as the storage container, to then grab x and y. Which gives 'undefined undefined', rather then '1 1' for the latter part.
class Position
constructor:(@x,@y) ->
x = [new Position(i,1) for i in [1]]
for i in x
alert i.x + ' ' + i.y#'undefined undefined'
i = new Position(1,1)
alert i.x + ' ' + i.y#'1 1'
能够使用点极其需要的清单,我无法找到一种方法,让他们的名单。我想preFER用一个简单的多维数组,但我不知道怎么办。
Being able to use a list of points is extremely needed and I cannot find a way to make a list of them. I would prefer to use a simple multidimensional array, but I don't know how.
推荐答案
您只需要使用括号,()
,而不是方括号 []
。
You just need to use parenthesis, ()
, instead of square brackets, []
.
从REPL:
coffee> ([i, 1] for i in [1])
[ [ 1, 1 ] ]
coffee> [[i, 1] for i in [1]]
[ [ [ 1, 1 ] ] ]
您可以看到,使用方括号,你会在Python,提出一个额外的名单内的发电前pression。
you can see that using the square brackets, as you would in Python, puts the generating expression inside of an extra list.
这是因为括号()
实际上只是在CoffeeScript的那里,当你想除权pression分配给一个变量,所以:
This is because the parenthesis, ()
are actually only there in CoffeeScript for when you want to assign the expression to a variable, so:
coffee> a = ([i, 1] for i in [1])
[ [ 1, 1 ] ]
coffee> a[0][1]
1
coffee> b = [i, 1] for i in [1]
[ [ 1, 1 ] ]
coffee> b[0][1]
undefined
此外,请参阅。
这篇关于咖啡脚本多维数组创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!