Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining an array of anonymous objects in CoffeeScript

Tags:

coffeescript

How do I define an array of anonymous objects in CoffeeScript? Is this possible at all, using the YAML syntax?

I know that having an array of named objects is quite easy:

items:[
   item1:
      name1:value1
   item2:
      name:value2
]

However, it would be a bit trickier, if those two objects had no names

like image 773
Preslav Rachev Avatar asked Jan 27 '12 05:01

Preslav Rachev


3 Answers

Simple -- place a comma by itself in a column lower than that in which you define your objects.

a = [
     nameA1: valueA1
     nameA2: valueA2
     nameA3: valueA3
  ,
     nameB1: valueB1
     nameB2: valueB2
     nameB3: valueB3
]

Will become:

var a;

a = [
  {
    nameA1: valueA1,
    nameA2: valueA2,
    nameA3: valueA3
  }, {
    nameB1: valueB1,
    nameB2: valueB2,
    nameB3: valueB3
  }
];
like image 111
Michael Hays Avatar answered Nov 13 '22 21:11

Michael Hays


You can also add a coma between each object: 

items:[
    item1:
        name1:value1
  ,
    item2:
        name:value2
]
like image 23
arthur Avatar answered Nov 13 '22 20:11

arthur


you can't:

this is some tricks:

items:[
    (name:"value1")
    (name:"value2")
]

another

items:[
    true && name:"value1"
    true && name:"value2"
]

this is the best:

items:[
    {name:"value1"}
    {name:"value2"}
]
like image 27
island205 Avatar answered Nov 13 '22 20:11

island205