2014-02-06 89 views
3

此代碼:爲什麼我無法鏈接這一系列的lodash調用?

_(gameState.loot) 
    .pick((value, key) -> key isnt "pickUpAnimations") 
    .filter ((d) -> _.isArray (d)) 
    .reduce ((sum, d) -> sum.concat (d)) 

是給我這個錯誤:

TypeError: 'undefined' is not a function (evaluating '(function(d) { 

鑑於此代碼工作正常:

removedAnimations = _.pick(gameState.loot, (value, key) -> key isnt "pickUpAnimations") 
    removedAnimations = _.filter removedAnimations, ((d) -> _.isArray (d)) 
    removedAnimations = _.reduce removedAnimations, ((sum, d) -> sum.concat (d)) 
    removedAnimations 

對我來說,似乎這些應該做同樣的事情。的gameState.loot架構是這樣的:

loot: { 
    ttl: 6000 
    slowBlinkWhenTtlLessThanPercent: 60 
    fastBlinkWhenTtlLessThanPercent: 30 
    resurrections: [] 
    gold: [] 
    health: [] 
    equipment: [] 
    pickUpAnimations: [] 
} 

順便說一句,這是第一個例子中所產生的JavaScript:

return _(gameState.loot).pick(function(value, key) { 
    return key !== "pickUpAnimations"; 
}).filter((function(d) { 
    return _.isArray(d); 
}).reduce((function(sum, d) { 
    return sum.concat(d); 
}))); 

我試圖@這個Blender的建議:

_(gameState.loot) 
    .pick (value, key) -> key isnt "pickUpAnimations" 
    .filter (d) -> _.isArray (d) 
    .reduce (sum, d) -> sum.concat (d) 

但是,這給了我這個錯誤:

>> TypeError: 'undefined' is not a function (evaluating '"pickUpAnimations".filter(function(d) { 

這裏的JavaScript的樣子:

return _(gameState.loot).pick(function(value, key) { 
    return key !== "pickUpAnimations".filter(function(d) { 
    return _.isArray(d.reduce(function(sum, d) { 
     return sum.concat(d); 
    })); 
    }); 
}); 
+0

不知道是什麼問題,但你可以刪除一些括號https://gist.github.com/elclanrs/8838052 – elclanrs

回答

9

這裏有更好的壓痕生成的JavaScript:

_(gameState.loot).pick(function(value, key) { 
    return key !== "pickUpAnimations"; 
}).filter(
    (function(d) { 
     return _.isArray(d); 
    }).reduce((function(sum, d) { 
     return sum.concat(d); 
    })) 
); 

正如你所看到的,.filter (....filter(...是不一樣的。兩種可能的解決方法:

_(gameState.loot) 
    .pick((value, key) -> key isnt "pickUpAnimations") 
    .filter((d) -> _.isArray (d)) 
    .reduce((sum, d) -> sum.concat (d)) 
  • 刪除括號完全:

    _(gameState.loot) 
        .pick (value, key) -> key isnt "pickUpAnimations" 
        .filter (d) -> _.isArray (d) 
        .reduce (sum, d) -> sum.concat (d) 
    
  • 您還可以得到

    1. 方法名和左括號之間取出空白擺脫匿名函數_.isArray

      _(gameState.loot) 
           .pick (value, key) -> key isnt "pickUpAnimations" 
           .filter _.isArray 
           .reduce (sum, d) -> sum.concat (d) 
      
    +0

    我正在查看生成的代碼相比,我在上面的評論和是,這是確實是這個問題。 – elclanrs

    +0

    這個錯誤以不同的方式給我。請參閱我的問題中的編輯。 –

    +0

    感謝您的幫助,並在javascript中看到,我意識到我沒有像以前那樣使用Coffeescript 1.7.1。我不得不更新我的grunt-contrib-coffeescript插件。 –

    2

    陰暗面的CoffeeScript的:

    _(gameState.loot).pick(function(value, key) { 
        return key !== "pickUpAnimations"; 
    }).filter((function(d) { 
        return _.isArray(d); 
    }).reduce((function(sum, d) { 
        return sum.concat(d); 
    }))); 
    

    注意額外(您的過濾回調之前。發生了什麼是你打電話:

    (function(d) { return _.isArray(d); }).reduce(... 
    

    哪個會失敗。修好你的捲髮,你很好走。

    相關問題