我使用ajax來提出請求。作爲請求的一部分,我得到一個嵌套json中的ID。像這樣:如何使用'。'來轉義變量在jQuery/JavaScript?
{"id": 1,"point": ["2016-10-13 09:05:53",85],"id": 1,"point": ["2016-10-13 09:05:53",85]}
然後我用一個forEach
循環來獲取所有數據:
success: function(data) {
data.forEach(function(chartData) {
var series = window.chart1.series[0],
});
},
我想使用該ID在JSON作爲一個變量。到目前爲止,我已經試過:
var series = window.chart+chartData.id+.series[0]
,但我得到一個錯誤:
Uncaught TypeError: Cannot read property '0' of undefined
當我直接輸入ID:
var series = window.chart1.series[0],
運行沒有任何問題,如果我安慰.log(chartData.id)我得到正確的ID 1
。
如何在json中使用嵌套ID作爲變量?
您可能在賦值給它之前使用'window.chart1'變量。請參閱[如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tushar