javascript - Why can't I chain this series of lodash calls? -


this code:

  _(gamestate.loot)   .pick((value, key) -> key isnt "pickupanimations")   .filter ((d) -> _.isarray (d))   .reduce ((sum, d) -> sum.concat (d)) 

is giving me error:

typeerror: 'undefined' not function (evaluating '(function(d) { 

whereas code works fine:

  removedanimations = _.pick(gamestate.loot, (value, key) -> key isnt "pickupanimations")   removedanimations = _.filter removedanimations, ((d) -> _.isarray (d))   removedanimations = _.reduce removedanimations, ((sum, d) -> sum.concat (d))   removedanimations 

to me seems these should doing same thing. schema of gamestate.loot looks this:

loot: {   ttl: 6000   slowblinkwhenttllessthanpercent: 60   fastblinkwhenttllessthanpercent: 30   resurrections: []   gold: []   health: []   equipment: []   pickupanimations: [] } 

btw, javascript being generated first example:

return _(gamestate.loot).pick(function(value, key) {   return key !== "pickupanimations"; }).filter((function(d) {   return _.isarray(d); }).reduce((function(sum, d) {   return sum.concat(d); }))); 

i tried @blender's suggestion of this:

  _(gamestate.loot)     .pick (value, key) -> key isnt "pickupanimations"     .filter (d) -> _.isarray (d)     .reduce (sum, d) -> sum.concat (d) 

but gave me error:

>> typeerror: 'undefined' not function (evaluating '"pickupanimations".filter(function(d) { 

here's how javascript looks like:

return _(gamestate.loot).pick(function(value, key) {   return key !== "pickupanimations".filter(function(d) {     return _.isarray(d.reduce(function(sum, d) {       return sum.concat(d);     }));   }); }); 

here's generated javascript better indentation:

_(gamestate.loot).pick(function(value, key) {     return key !== "pickupanimations"; }).filter(     (function(d) {         return _.isarray(d);     }).reduce((function(sum, d) {         return sum.concat(d);     })) ); 

as can see, .filter (... , .filter(... not same. 2 possible fixes:

  1. remove whitespace between method names , opening parentheses:

    _(gamestate.loot)     .pick((value, key) -> key isnt "pickupanimations")     .filter((d) -> _.isarray (d))     .reduce((sum, d) -> sum.concat (d)) 
  2. remove parentheses entirely:

    _(gamestate.loot)     .pick (value, key) -> key isnt "pickupanimations"     .filter (d) -> _.isarray (d)     .reduce (sum, d) -> sum.concat (d) 

you can rid of anonymous function calling _.isarray:

    _(gamestate.loot)         .pick (value, key) -> key isnt "pickupanimations"         .filter _.isarray         .reduce (sum, d) -> sum.concat (d) 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -