2
檢索來自物體的值我已經對象如何通過使變量作爲密鑰
position = [
{exchange: 'KRAKEN', USD: 1800, EUR: 800, GBP: 800, BTC: 800},
{exchange: 'BTCE', USD: 800, EUR: 800, GBP: 800, BTC: 800},
{exchange: 'BITSTAMP', USD: 600, EUR: 800, GBP: 800, BTC: 800},
{exchange: 'MYWALLET', USD: 1300, EUR: 800, GBP: 800, BTC: 800}
]
的下面陣列I也有2個變量: myExchange和myCurr
我試圖從數組中提取相關貨幣值。 即如果myExchange ='KRAKEN'和myCurr = USD,那麼我需要獲取結果= 1800
我在coffeescript(和下劃線._作爲庫)中使用以下代碼,但它返回爲' undefined'
objBuy = _.find(position, (objBuy) ->
objBuy.exchange is buyExchange
)
objBuyCurr = _.find(objBuy, (objBuyCurr) ->
objBuy._key is buyCurr
)
最終我的目標是將對象屬性/鍵作爲變量動態傳遞。即: 值= position.myExchange.myCurr(不position.KRAKEN.USD)
謝謝Suseika。然而我需要的是能夠說:position.myExchange.myCurr。即動態地變換我們的變量。這可能嗎? – 2015-02-09 15:51:25
@saurabhgoyal我更新了答案 – gvlasov 2015-02-09 15:58:57