0

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

3 Answers 3

1

You don't need , for specifying element of array.

params = 
  zone: "test"
  currency: "USD"
  products: [
    {
      name: "product"
      ID: "id"
    }
    {
      name: "product2"
      ID: "id2"          
    }
  ]
Sign up to request clarification or add additional context in comments.

Comments

1

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"          
  ]

Comments

0

This answer is to point the tricky part in @Markus answer.

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' } ]

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' } ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.