coffeescript - Coffescript creating a hashtable -


in following code segment i'm trying create hashtable single key named "one" , push same value "ted" array.

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

what missing? seems output is:

pushing ted pushing ted pushing ted pushing ted pushing ted pushing ted pushing ted pushing ted pushing ted pushing ted { one: [ 'ted' ] } 

i expect output be:

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

here fiddle: http://jsfiddle.net/u4wpg4ts/

coffeescript's in keyword doesn't mean same in javascript. check presence of value rather of key.

# coffee if (key not in out) 
// .js (roughly) indexof = array.prototype.indexof;  if (indexof.call(out, key) < 0) 

since key ("one") never present in array value ("ted"), condition passes. so, array being replaced , reset empty before each .push().

coffeescript's of keyword instead check key's presence, should pass first time:

# coffee if (key not of out) 
// .js if (!(key in out)) 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -