[英]add Array of Object in an object coffeescript
I wish to add an array of objects in an object in coffeescript can't seem to be doing this Here's what I've tried :我希望在咖啡脚本中的对象中添加一组对象似乎无法这样做这是我尝试过的:
params =
zone: "test"
currency: "USD"
products: [
{
name: "product"
ID: "id"
},
{
name: "product2"
ID: "id2"
}
]
the problem is on the products part.问题出在产品部分。
thanks谢谢
You don't need ,
for specifying element of array.您不需要,
来指定数组元素。
params =
zone: "test"
currency: "USD"
products: [
{
name: "product"
ID: "id"
}
{
name: "product2"
ID: "id2"
}
]
What you posted compiles fine for me.你发布的内容对我来说很好。
Instead of losing the ,
, you can lose the brackets:您可以丢失括号而不是丢失,
:
params =
zone: "test"
currency: "USD"
products: [
name: "product"
ID: "id"
,
name: "product2"
ID: "id2"
]
This answer is to point the tricky part in @Markus answer.这个答案是指出@Markus 答案中棘手的部分。
Notice the ,
between the two objects, which is one level outer in indentation than the objects.请注意,
两个对象之间的 , 比对象缩进一级。
[
name: "product"
ID: "id"
,
name: "product2"
ID: "id2"
]
this would result in: [ { name: 'product', ID: 'id' }, { name: 'product2', ID: 'id2' } ]
这将导致: [ { name: 'product', ID: 'id' }, { name: 'product2', ID: 'id2' } ]
Whereas putting the comma in the same level as the object而将逗号放在与对象相同的级别
[
name: "product"
ID: "id"
,
name: "product2"
ID: "id2"
]
results in [ { name: 'product2', ID: 'id2' } ]
结果在[ { name: 'product2', ID: 'id2' } ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.