0

I am fighting with Javascript data structures. I need to create a map where the key is a string and the value is an array of two longs.

For example, something like:

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..

How do can I achieve this properly in Javascript?

4
  • What do you mean by long?
    – VisioN
    Mar 25, 2013 at 12:33
  • Something that can contain timestamps (milliseconds since the epoch) Mar 25, 2013 at 12:33
  • @JVerstry en.wikipedia.org/wiki/JSON has more information
    – rab
    Mar 25, 2013 at 12:45
  • what is "array of two longs" ? Mar 25, 2013 at 12:50

3 Answers 3

1

So, in order to work with multidimensional arrays, you need first to "define" the dimensions, i.e.

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;

The same can be done with literal:

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

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

dictionary equivalent data structure in javascript is basic JavaScript object . you can try

var myMap = {

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

and for print them

0

Assuming you don't actually mean a 2d array;

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

alert( myMap["SomeString"][0] );
alert( myMap["SomeString"][1] );
1
  • Yes I do, the returned array is an array of int/long pairs Mar 25, 2013 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.