Coffeescript 创建哈希表的Coffescript

Coffeescript 创建哈希表的Coffescript,coffeescript,Coffeescript,在下面的代码段中,我试图创建一个名为“one”的散列表,并将相同的值“ted”推送到数组中 out = {}; for i in [1..10] key = "one"; if(key not in out) out[key] = []; out[key].push("ted") console.log("pushing ted"); console.log(out); 我错过了什么?输出结果似乎是: pushing ted pushing ted pushing t

在下面的代码段中,我试图创建一个名为“one”的散列表,并将相同的值“ted”推送到数组中

out = {};
for i in [1..10]
  key = "one";
  if(key not in out)
    out[key] = [];
  out[key].push("ted")
  console.log("pushing ted");

console.log(out);
我错过了什么?输出结果似乎是:

pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
{ one: [ 'ted' ] }
我预计产出为:

pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
pushing ted
{ one: [ 'ted','ted','ted','ted','ted','ted','ted','ted','ted','ted' ] }
这是一把小提琴:

CoffeeScript在关键字中的
与JavaScript中的含义不同。它将检查是否存在值而不是键

# coffee
if (key not in out)

您也可以使用
out[key]?=[]
out[key]| |=[]
代替
的if
。甚至是
(out[key]?=[])。按('ted')
(out[key]|=[])。按('ted')
// .js (roughly)
indexOf = Array.prototype.indexOf;

if (indexOf.call(out, key) < 0)
# coffee
if (key not of out)
// .js
if (!(key in out))