![](/img/trans.png)
[英]Javascript - object with key value pair, where value is an array
[英]Map with String key where value is array of pair of longs in Javascript?
我正在與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中正確實現這一點?
javascript中的字典等價數據結構是基本的JavaScript對象。 你可以試試
var myMap = {
SomeString : [
[1,2],
[32222,44444]
]
}
並打印它們
因此,為了使用多維數組,首先需要“定義”維度,即
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
假設你實際上並不是指二維數組;
var myMap = {};
myMap["SomeString"] = [123, 456];
alert( myMap["SomeString"][0] );
alert( myMap["SomeString"][1] );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.