IT story

CoffeeScript에서 익명 개체 배열 정의

hot-time 2020. 8. 10. 08:09
반응형

CoffeeScript에서 익명 개체 배열 정의


CoffeeScript에서 익명 개체 배열을 어떻게 정의합니까? YAML 구문을 사용하여 가능합니까?

명명 된 개체의 배열을 갖는 것이 매우 쉽다는 것을 알고 있습니다.

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

하지만이 두 개체에 이름이 없으면 조금 더 까다로울 것입니다.


당신은 할 수 없습니다 :

이것은 몇 가지 트릭입니다.

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

다른

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

이게 최선이다:

items:[
    {name:"value1"}
    {name:"value2"}
]

단순-개체를 정의하는 열보다 낮은 열에 쉼표 만 넣으십시오.

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

될 것입니다:

var a;

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

각 개체 사이에 코마를 추가 할 수도 있습니다. 

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

쉼표 솔루션이 더 낫다고 생각하지만 완전성을 위해 이것을 추가 할 것이라고 생각했습니다.

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

배열을 정의하는 동안 변수를 정의 할 수 있으므로 추악한 대답은 다음과 같습니다.

a = 
  items: [
    item1 = 
      name: 'value1'
    item2 = 
      name: 'value2'
  ]

작동하지만 "정의되었지만 사용되지 않은 변수 (item1, item2)"에 대한 경고가 표시 될 수 있습니다. 더 나은 방법은 사용하지 않는 변수를 생략하는 데 사용되는 밑줄, 변수를 사용하는 것입니다.

a = 
  items: [
    _ = 
      name: 'value1'
    _ = 
      name: 'value2'
  ]

console.log JSON.stringify(a) will produce this:

  {
    "items":[
      {
        "name":"value1"
      },{
        "name":"value2"
      }
    ]
  }

Not an answer to the OP's question, but just in case you're here for the same reason I was... If you're low on Mountain Dew and use '=' instead of ':', then Coffeescript will turn your array of objects into a flat array without a compile error:

data = [
    one='one'
    two='two'
  ,
    one='1'
    two='2'
]

Produces

['one', 'two', '1', '2']

Insert more Mountain Dew and replace the '=' with ':'.


I'm very happy to report after a bit of fiddling that I could get this to compile just right:

items: [
  nameA: subA
  nameB: subB
,
  nameX: subX
  nameY: subY
]

It results it just what you'd expect: a list of two anonymous objects.


I ran into a related problem and found this solution. If you want an array of many single k/v objects without braces, just indent some of them. Seems to do the trick.

data = [                                     
  "2013-09-25T16:46:52.636Z":3,              
    "2013-09-25T16:47:52.636Z":6,            
      "2013-09-25T16:48:52.636Z":2,          
        "2013-09-25T16:49:52.636Z":7,        
  "2013-09-25T16:50:52.636Z":5,              
    "2013-09-25T16:51:52.636Z":2,            
      "2013-09-25T16:52:52.636Z":1,          
        "2013-09-25T16:53:52.636Z":3,        
  "2013-09-25T16:54:52.636Z":8,              
    "2013-09-25T16:55:52.636Z":9,            
      "2013-09-25T16:56:52.636Z":2,          
        "2013-09-25T16:57:52.636Z":5,        
          "2013-09-25T16:58:52.636Z":7       
]                                            

Produces:

coffee> data
[ { '2013-09-25T16:46:52.636Z': 3 },
  { '2013-09-25T16:47:52.636Z': 6 },
  { '2013-09-25T16:48:52.636Z': 2 },
  { '2013-09-25T16:49:52.636Z': 7 },
  { '2013-09-25T16:50:52.636Z': 5 },
  { '2013-09-25T16:51:52.636Z': 2 },
  { '2013-09-25T16:52:52.636Z': 1 },
  { '2013-09-25T16:53:52.636Z': 3 },
  { '2013-09-25T16:54:52.636Z': 8 },
  { '2013-09-25T16:55:52.636Z': 9 },
  { '2013-09-25T16:56:52.636Z': 2 },
  { '2013-09-25T16:57:52.636Z': 5 },
  { '2013-09-25T16:58:52.636Z': 7 } ]

It's counter-intuitive to me; you'd think that this would make sub-objects but I think the comma at the end of the line tells it to stop making properties on that object.


Why not:

list = []
list.push
  prop1: val
  prop2: val
list.push
  prop1: val
  prop2: val

It's still a huge improvement to me over js, very easy to read, minimal and pretty safe to write.

참고URL : https://stackoverflow.com/questions/9029481/defining-an-array-of-anonymous-objects-in-coffeescript

반응형