CoffeeScript 2 Dimensional Array Usage
Posted
by
Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2012-09-27T03:28:53Z
Indexed on
2012/09/27
3:37 UTC
Read the original article
Hit count: 137
coffeescript
I feel like I'm missing something with CoffeeScript and 2 dimensional arrays. I'm simply attempting to make a grid of spaces (think checkers). After some searching and a discovery with the arrays.map function, I came up with this:
@spaces = [0...20].map (x)->
[0...20].map (y) ->
new Elements.Space()
And this seems to work great, I have a nice 2 dimensional array with my Space object created in each. But now I want to send the created space constructor the x,y location. Because I'm two layers deep, I lost the x
variable when I entered the map function for y
.
Ideally I would want to do something like:
@spaces = [0...20].map (x)->
[0...20].map (y) ->
new Elements.Space(x, y)
or something that feels more natural to me like:
for row in rows
for column in row
@spaces[row][column] = new Elements.Space(row, column)
I'm really open to any better way of doing this. I know how I would do it in standard JavaScript, but really would like to learn how to do it in CoffeeScript.
© Stack Overflow or respective owner