在 CoffeeScript 中,如何使用变量作为哈希中的键?

发布于 2024-12-08 20:17:38 字数 308 浏览 0 评论 0原文

例如:

wtf

所以:

foo = "asdf"
{foo: "bar"}
eval foo

# how do I get {"asdf": "bar"} ?

# this will throw parse error:
{(eval foo): "bar"}

这是一个简单的语法问题:如何让 CoffeeScript 动态构造哈希,而不是手工完成?

eg:

wtf

So:

foo = "asdf"
{foo: "bar"}
eval foo

# how do I get {"asdf": "bar"} ?

# this will throw parse error:
{(eval foo): "bar"}

This is a simple syntax question: how do I get CoffeeScript to construct a hash dynamically, rather than doing it by hand?

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

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

发布评论

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

评论(6

因为看清所以看轻 2024-12-15 20:17:39

CoffeeScript 与 JavaScript 一样,不允许您使用表达式/变量作为对象文本中的键。这是短暂的支持,但在 0.9.6 版本中被删除。创建对象后需要设置属性。

foo = 'asdf'

x = {}
x[foo] = 'bar'
alert x.asdf # Displays 'bar'

CoffeeScript, like JavaScript, does not let you use expressions/variables as keys in object literals. This was support briefly, but was removed in version 0.9.6. You need to set the property after creating the object.

foo = 'asdf'

x = {}
x[foo] = 'bar'
alert x.asdf # Displays 'bar'
骑趴 2024-12-15 20:17:39

虽然有点丑,但仍然是一句台词(抱歉迟到了):

{ "#{foo}": bar }

Somewhat ugly but a one-liner nonetheless (sorry for being late):

{ "#{foo}": bar }

往事随风而去 2024-12-15 20:17:39

如果您希望使用 Coffeescript 的最小语法来定义关联数组,我建议您创建一个简单的两行方法,在定义数组后将变量名称键转换为变量值。

我是这样做的(实际数组要大得多):

@sampleEvents = 
   session_started:
          K_TYPE: 'session_started'
          K_ACTIVITY_ID: 'activity'

   session_ended:
          K_TYPE: 'session_ended'

   question_answered:
          K_TYPE: 'question_answered'
          K_QUESTION: '1 + 3 = '
          K_STUDENT_A: '3'
          K_CORRECT_A: '4' #optional
          K_CORRECTNESS: 1 #optional
          K_SECONDS: 10 #optional
          K_DIFFICULTY: 4 #optional


for k, event of @sampleEvents
    for key, value of event
        delete event[key]
        event[eval(key.toString())] = value

SampleEvents 数组现在是:

{ session_started: 
   { t: 'session_started',
     aid: 'activity',
     time: 1347777946.554,
     sid: 1 },
  session_ended: 
   { t: 'session_ended', 
     time: 1347777946.554, 
     sid: 1 },
  question_answered: 
   { t: 'question_answered',
     q: '1 + 3 = ',
     sa: '3',
     ca: '4',
     c: 1,
     sec: 10,
     d: 4,
     time: 1347777946.554,
     sid: 1 },

If you're looking to use Coffeescript's minimal syntax for defining your associative array, I suggest creating a simple two line method to convert the variable name keys into the variable values after you've defined the array.

Here's how I do it (real array is much larger):

@sampleEvents = 
   session_started:
          K_TYPE: 'session_started'
          K_ACTIVITY_ID: 'activity'

   session_ended:
          K_TYPE: 'session_ended'

   question_answered:
          K_TYPE: 'question_answered'
          K_QUESTION: '1 + 3 = '
          K_STUDENT_A: '3'
          K_CORRECT_A: '4' #optional
          K_CORRECTNESS: 1 #optional
          K_SECONDS: 10 #optional
          K_DIFFICULTY: 4 #optional


for k, event of @sampleEvents
    for key, value of event
        delete event[key]
        event[eval(key.toString())] = value

The SampleEvents array is now:

{ session_started: 
   { t: 'session_started',
     aid: 'activity',
     time: 1347777946.554,
     sid: 1 },
  session_ended: 
   { t: 'session_ended', 
     time: 1347777946.554, 
     sid: 1 },
  question_answered: 
   { t: 'question_answered',
     q: '1 + 3 = ',
     sa: '3',
     ca: '4',
     c: 1,
     sec: 10,
     d: 4,
     time: 1347777946.554,
     sid: 1 },
仅冇旳回忆 2024-12-15 20:17:39

试试这个:

foo = "asdf"

eval "var x = {#{foo}: 'bar'}"
alert(x.asdf)

Try this:

foo = "asdf"

eval "var x = {#{foo}: 'bar'}"
alert(x.asdf)
山有枢 2024-12-15 20:17:38

对于将来发现这个问题的任何人,从 CoffeeScript 1.9.1 开始,支持内插对象文字键!

语法如下所示:

myObject =
  a: 1
  "#{ 1 + 2 }": 3

请参阅 https://github.com/jashkenas/coffeescript/commit/76c076db555c9ac7c325c3b285cd74644a9bf0d2

For anyone that finds this question in the future, as of CoffeeScript 1.9.1 interpolated object literal keys are supported!

The syntax looks like this:

myObject =
  a: 1
  "#{ 1 + 2 }": 3

See https://github.com/jashkenas/coffeescript/commit/76c076db555c9ac7c325c3b285cd74644a9bf0d2

誰認得朕 2024-12-15 20:17:38

你为什么要使用eval?您可以按照与 JavaScript 中完全相同的方式进行操作:

foo    = 'asdf'
h      = { }
h[foo] = 'bar'

翻译为 JavaScript:

var foo, h;
foo = 'asdf';
h = {};
h[foo] = 'bar';

结果是 h 看起来像 {'asdf': 'bar'}< /代码>。

Why are you using eval at all? You can do it exactly the same way you'd do it in JavaScript:

foo    = 'asdf'
h      = { }
h[foo] = 'bar'

That translates to this JavaScript:

var foo, h;
foo = 'asdf';
h = {};
h[foo] = 'bar';

And the result is that h looks like {'asdf': 'bar'}.

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