2013-04-04 78 views
0

我試圖用對象創建一個關聯數組,鍵應始終爲字符串(但它們總是數字)。 這是我如何保存它們(記錄用戶點擊):無法用STRING鍵創建數組

App.Recording[currentTime.toString()] = {sound: buttonName.toLowerCase() }; 

當試圖做到這一點:

var save = {}; 

save.recording = App.Recording; 
console.log(JSON.stringify(save)); 

我得到這個:

{"recording":[null, null,{"sound":"e"},null,null,null,.......,null,null,null,null,{"sound":"e"},....,null, null...]} 

因此,toString()不起作用在currentTime.toString(),這使我的數組存儲currentTime作爲數字,而不是...

如何保存對象並創建關聯數組?

回答

1

先看看HERE。 JS中沒有關聯數組。您應該使用帶有for in循環的對象來代替數組。