如何在CoffeeScript中创建对象对象?

时间:2015-01-22 00:10:57

标签: coffeescript

我正在尝试创建一个由其他对象组成的对象。我无法弄清楚语法。我在CoffeeScript上阅读了Little Book,但它没有说明在这种情况下该怎么做。我尝试了以下语法,但无法正确使用它:

  objectOfHomes = {
    {
      location: "Paris Island", age: 18
    }
  }

  objectOfHomes:
      {location: "Paris Island", age: 18},
      {location: "29 Palms", age: 18},
      {location: "Camp Lejeune", age: 19},
      {location: "Iraq", age: 20},
      {location: "Camp Lejeune", age: 20},
      {location: "Mesa Verda", age: 22}


  objectOfHomes =  
    {location: "Paris Island", age: 18}
    {location: "29 Palms", age: 18}
    {location: "Camp Lejeune", age: 19}
    {location: "Iraq", age: 20}
    {location: "Camp Lejeune", age: 20}
    {location: "Mesa Verda", age: 22}

3 个答案:

答案 0 :(得分:0)

在coffeescript中你可以这样做:

objectOfHomes:
  a:
    location: "Paris Island"
    age: 18

答案 1 :(得分:0)

使用对象数组比使用对象对象更好,除非你有一些你似乎没有的主键。

 objectOfHomes = [
     {location: "Paris Island", age: 18}
     {location: "29 Palms", age: 18}
     {location: "Camp Lejeune", age: 19}
     {location: "Iraq", age: 20}
     {location: "Camp Lejeune", age: 20}
     {location: "Mesa Verda", age: 22}
 ]

答案 2 :(得分:-1)

您需要在对象中包含键值:

  objectOfHomes: 
    a: {location: "Paris Island", age: 18}
    b: {location: "29 Palms", age: 18}
    c: {location: "Camp Lejeune", age: 19}
    d: {location: "Iraq", age: 20}
    e: {location: "Camp Lejeune", age: 20}
    f: {location: "Mesa Verda", age: 22}