2013-03-25 72 views
0

我正在與JavaScript數據結構戰鬥。我需要創建一個映射,其中的鍵是一個字符串,並且該值是一個包含兩個long的數組。Map with String key where value是Javascript中long對的數組嗎?

例如,像:

var x = myMap["SomeString"]; 

var firstLong = x[0][0]; 
var secondLong = x[0][1]; 

// do something with first and second long 

firstLong = x[1][0]; 
secondLong = x[1][1]; 

// do something with first and second long 

etc.. 

如何能正確我在Javascript中實現這一目標?

+0

什麼你的意思是「長」嗎? – VisioN 2013-03-25 12:33:10

+0

可以包含時間戳的東西(自時代以來的毫秒數) – JVerstry 2013-03-25 12:33:52

+0

@JVerstry http://en.wikipedia.org/wiki/JSON有更多信息 – rab 2013-03-25 12:45:54

回答

1

所以,爲了與多維數組工作,你需要先「定義」的尺寸,即

var myMap = {}; 
myMap["SomeString"] = []; 

myMap["SomeString"][0] = []; // new dimension 
myMap["SomeString"][0][0] = 1; // can be also done with 
myMap["SomeString"][0][1] = 2; // myMap["SomeString"][0].push(2); 

myMap["SomeString"][1] = []; // new dimension 
myMap["SomeString"][1][0] = 3; 
myMap["SomeString"][1][1] = 4; 

這也可以用文字來完成:

var myMap = { 
    SomeString: [ 
     [1, 2], 
     [3, 4] 
    ] 
}; 

console.log(myMap["SomeString"][0][1]); // 2 
1

javascript中的字典等效數據結構是基本的JavaScript對象。你可以嘗試

var myMap = { 

    SomeString : [ 
     [1,2], 
     [32222,44444] 
    ] 
} 

和打印他們

0

假設你實際上並不意味着一個二維數組;

var myMap = {}; 
myMap["SomeString"] = [123, 456]; 

alert(myMap["SomeString"][0]); 
alert(myMap["SomeString"][1]); 
+0

是的,返回的數組是int/long對的數組 – JVerstry 2013-03-25 12:39:48

相關問題