Coffeescript 如何生成数组的数组?

Coffeescript 如何生成数组的数组?,coffeescript,Coffeescript,我正试图用coffescript做类似的事情,但它不起作用 locations = [59.32522, 18.07002] [59.327383, 18.06747] 哦,我想出来了 locations = [ [59.32522, 18.07002] [59.327383, 18.06747] ] 我知道你为自己的问题找到了解决办法,而这并不是你要找的柯克的确切答案。但是在Ruby中有一个我喜欢的任意哈希对象(注意它们比固定维数组更占用内存) 发件人: 作者:塞巴斯蒂安·

我正试图用
coffescript
做类似的事情,但它不起作用

locations =
  [59.32522, 18.07002]
  [59.327383, 18.06747]

哦,我想出来了

locations = [
  [59.32522, 18.07002]
  [59.327383, 18.06747]
]

我知道你为自己的问题找到了解决办法,而这并不是你要找的柯克的确切答案。但是在Ruby中有一个我喜欢的任意哈希对象(注意它们比固定维数组更占用内存)

发件人: 作者:塞巴斯蒂安·亨格里克 这个结构的有趣之处在于,您可以使用它动态创建所需的任何类型的嵌套哈希(有点像在使用mkdir-p时创建子目录)。它与我们有一些共同的品质

让我们看看类似的对象在CoffeeScript中会是什么样子

x = 
  la:  
    li:  
      lu:  
        chunky:  
          bacon:  
            foo: 'bar' 

alert x['la']['li']['lu']['chunky']['bacon']['foo']


y = { la: { li: { lu: { chunky: { bacon: { foo:'bary' } } } } } }  

alert y['la']['li']['lu']['chunky']['bacon']['foo']
我还没能想出一个比纯JSON对象创建更干净的随用创建接口,因为在Javascript中不能重载括号运算符

好的,我提出了JSON语法的一个简短缩写,但它没有Ruby nestedHash好

Block = (obj,rest...) ->
  console.log 'obj',obj
  console.log 'rest',rest
  obj = {} if (typeof obj is "undefined") 
  if rest.length >= 2
    key = rest[0]
    obj[key] = Block(obj[key],rest[1...]...)
    obj
  else if rest.length is 1
    obj = rest[0]

z = new Block(z,'la','li','lu','chunky','bacon','foo','barz')
console.log z['la']['li']['lu']['chunky']['bacon']['foo']
# extend the object
z = new Block(z,'la','li','lu','chunky','bacon','fooz','ball')
console.log JSON.stringify(z)

# add a node to an internal hash
a = z['la']['li']['lu']
a = new Block(a,'chunky','bacon','another','node')
console.log 'a is',JSON.stringify(a)

# the node now exists on the parent as well
console.log 'z is',JSON.stringify(z)

你想创建一个二维数组?
Block = (obj,rest...) ->
  console.log 'obj',obj
  console.log 'rest',rest
  obj = {} if (typeof obj is "undefined") 
  if rest.length >= 2
    key = rest[0]
    obj[key] = Block(obj[key],rest[1...]...)
    obj
  else if rest.length is 1
    obj = rest[0]

z = new Block(z,'la','li','lu','chunky','bacon','foo','barz')
console.log z['la']['li']['lu']['chunky']['bacon']['foo']
# extend the object
z = new Block(z,'la','li','lu','chunky','bacon','fooz','ball')
console.log JSON.stringify(z)

# add a node to an internal hash
a = z['la']['li']['lu']
a = new Block(a,'chunky','bacon','another','node')
console.log 'a is',JSON.stringify(a)

# the node now exists on the parent as well
console.log 'z is',JSON.stringify(z)