2012-02-09 38 views
0

我需要創建一個更有組織的數據數組來解析我的模板 我怎麼能寫得更好?我想將數組分組到自己的數組中,例如Vars:[array],[array] ... mvData:[array],[array] ...等等。Javascript中多維數組的資源

我想成爲能夠使用vars.globalVars [2]/mvData.Revenue - 等...

我仍在學習數組 - 請參閱下面的內容。所有的數據只是象徵性..

vars = ['Game', 'Player', 'Social Stats']; 
mvData = ['22,359', '16,235', '10,938', '5,305', '12,588', '6,583'];  
evoMetric = ['Registered Players', 'Started Missions', 'Objects Bought', 'Combats', 'Guerrilla Combats', 'Teams Created', 'Guerillas created']; 
playerMetric = ['playerMetric', 'playerMetric', 'playerMetric', 'playerMetric', 'playerMetric', 'playerMetric', 'playerMetric']; 
socialMetric = ['Invitations', 'Gifts', 'Combats', 'Comic', 'Trailer', 'Illustration', 'Mission', 'Levels']; 
+1

數組不是正確的解決方案;使用對象。 – 2012-02-09 19:39:46

回答

1

在Javascript中,你可以輕鬆地創建複雜的數據結構。以下是有效的對象

myObject = {"property1": "value1", 
      "property2": "value2", 
      "subobject": {"subproperty": "subvalue"}, 
      "aubarray": ["element1", "element2", {"innerobjectkey": "innerobjectvalue"}]}; 
+0

謝謝!出於某種原因,所有的搜索對我來說都不夠清楚 – iamwhitebox 2012-02-09 19:41:45

0

,你可以做這樣的

var MyArray = ['some text',[second dimension array],[second dimension array,[third dimension array] ]]; 
+0

並調用第二維數組,你會這樣做:MyArray [1] [0],像這樣的第三維數組:MyArray [2] [1] [索引你想要的]更多括號更多維度 – zero 2012-02-09 19:40:13

0

你可以構建它作爲一個對象:

{ 
vars: ['Game', 'Player', 'Social Stats'], 
mvData: ['22,359', '16,235', '10,938', '5,305', '12,588', '6,583'], 
evoMetric: ['Registered Players', 'Started Missions', 'Objects Bought', 'Combats', 'Guerrilla Combats', 'Teams Created', 'Guerillas created'], 
playerMetric: ['playerMetric', 'playerMetric', 'playerMetric', 'playerMetric', 'playerMetric', 'playerMetric', 'playerMetric'], 
socialMetric: ['Invitations', 'Gifts', 'Combats', 'Comic', 'Trailer', 'Illustration', 'Mission', 'Levels'] 
}