我正在尝试使用coffescript
执行此类操作,但它无效...
locations =
[59.32522, 18.07002]
[59.327383, 18.06747]
答案 0 :(得分:2)
哦,我明白了..
locations = [
[59.32522, 18.07002]
[59.327383, 18.06747]
]
答案 1 :(得分:1)
我意识到你为自己的问题找到了解决方案,而这正是你正在寻找Kirk的确切答案。但是在Ruby中有一个我喜欢的哈希对象的任意哈希(注意它们比固定维数组更加内存密集)。
blk = lambda {|h,k| h[k] = Hash.new(&blk)}
x = Hash.new(&blk)
x[:la][:li][:lu][:chunky][:bacon][:foo] = "bar"
这个结构的有趣之处在于你可以使用它创建你需要的任何类型的嵌套哈希(有点像你使用mkdir -p制作子目录)。它与JSON objects分享了一些特质。
让我们看一下类似的对象在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']
由于括号操作符无法在Javascript中重载,因此我无法提供更清晰的创建界面而不是纯JSON对象创建
好的,我想出了一个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)