1
在下面的代碼段中,我試圖用一個名爲「one」的鍵創建一個散列表,並將相同的值「ted」推送到一個數組中。Coffescript創建散列表
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' ] }
這裏是一個小提琴: http://jsfiddle.net/u4wpg4ts/
你也可以'出[關鍵] = []'或'出[?鍵] || = []'而不是'if'。甚至是'(out [key]?= [])。push('ted')'或者(out [key] || = [])。push('ted')'。 –