在 CoffeeScript 中定义匿名对象数组

发布于 2024-12-29 12:34:51 字数 201 浏览 2 评论 0原文

如何在 CoffeeScript 中定义匿名对象数组?使用 YAML 语法这是否可能?

我知道拥有一个命名对象数组非常容易:

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

但是,如果这两个对象没有名称,那就有点棘手了

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

对不⑦ 2025-01-05 12:34:51

简单——将一个逗号单独放置在比您定义对象的列低的列中。

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

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
  }
];
风和你 2025-01-05 12:34:51

您还可以在每个对象之间添加逗号:

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

You can also add a coma between each object: 

items:[
    item1:
        name1:value1
  ,
    item2:
        name:value2
]
那一片橙海, 2025-01-05 12:34:51

你不能:

这是一些技巧:

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

另一个

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

这是最好的:

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

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"}
]
倥絔 2025-01-05 12:34:51

我认为逗号解决方案更好,但我想为了完整性我应该添加这个:

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

I think the comma solution is better, but I figured I'd add this for completeness:

a = [
  {
    nameA1: valueA1
    nameA2: valueA2
    nameA3: valueA3
  }
  {
    nameB1: valueB1
    nameB2: valueB2
    nameB3: valueB3
  }
]
只是我以为 2025-01-05 12:34:51

您可以在定义数组时定义变量,因此一个丑陋的答案是:

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

它可以工作,但您可能会收到有关“已定义但未使用的变量(item1,item2)”的警告。更好的方法是使用下划线,变量用于省略未使用的变量:

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

console.log JSON.stringify(a) 将产生以下结果:

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

You can define variable while defining array, so an ugly answer would be:

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

It would work, but you may get warnings about "defined, but not used variables (item1, item2)". Better way would be to use underscore, variable used to omit not used variables:

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

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

  {
    "items":[
      {
        "name":"value1"
      },{
        "name":"value2"
      }
    ]
  }
恏ㄋ傷疤忘ㄋ疼 2025-01-05 12:34:51

我很高兴地报告说,经过一番摆弄后,我可以正确编译它:

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

它的结果正是您所期望的:两个匿名对象的列表。

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.

戴着白色围巾的女孩 2025-01-05 12:34:51

我遇到了一个相关问题并找到了这个解决方案。如果您想要一个由许多不带大括号的单个 k/v 对象组成的数组,只需缩进其中一些即可。似乎可以做到这一点。

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

这对我来说是违反直觉的;您可能认为这会创建子对象,但我认为行末尾的逗号告诉它停止在该对象上创建属性。

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.

断爱 2025-01-05 12:34:51

不是OP问题的答案,但以防万一你在这里的原因和我一样......如果你的激浪很低并使用'='而不是':',那么Coffeescript将转动你的数组将对象放入平面数组中而不会出现编译错误:

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

生成

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

插入更多 Mountain Dew 并将“=”替换为“:”。

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 ':'.

习ぎ惯性依靠 2025-01-05 12:34:51

为什么不呢:

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

与 js 相比,它对我来说仍然是一个巨大的改进,非常容易阅读、最小化并且编写起来非常安全。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文