2011-07-21 171 views
0

我想在JavaScript中有一個數組,每個項目包含2個屬性而不是1個,這怎麼可能?擁有2個屬性數組的最佳方式是什麼?

下默認情況下只增加一個屬性的項目:

var headerCellWidths = new Array(); 
headerCellWidths.push(100); 

這使我使用訪問項目[0] [normalCellWidth]

,但我希望能有例如[index] [normalCellWidth] [firstTemplateCellWidth]

例如, [0] [100] [25]

例如, headerCellWidths.add(100,25);

一個解決方案是創建我自己的CustomArray顯然它維護2個獨立的數組實例,但沒有更好的方法嗎?

謝謝,

+0

它沒有任何意義。只需使用像myObj.add(100,25),myObj.get(0,100,25)這樣的方法創建對象並將所有邏輯放在此處。 – kan

回答

5

您可以將條目作爲對象添加到數組中。 試試這個:

headerCellWidths.push({normalCellWidth:100, firstTemplateCellWidth:25); 

你會使用訪問項目:

headerCellWidths[0].normalCellWidthheaderCellWidths[0].firstTemplateCellWidth

+0

不錯,非常感謝。 –

相關問題