12

Can someone please redirect me to the right link or give an example of how to work with two dimensional array or HashTable in JQuery? I tried google but did not get the answer. I want to avoid using any plugins. All i want to do it, store some information and retrieve them like HashTable way.

2
  • Is your two dim. array like a table or more like a matrix? Commented Aug 3, 2010 at 8:30
  • It is kind of a hashtable which contains key value pair. Since there is no HashTable thing in Jquery, i was thinking two dim array will be better solution. I came across jhashtable (timdown.co.uk/jshashtable) just now. Wondering if it is OK to use it in the project. Commented Aug 3, 2010 at 8:39

3 Answers 3

30

Depending on what you want to use as keys into your "hashtable", you might want to use an object with array properties instead of a two dimensional array.

For instance:

var hashtable = {};
hashtable['screaming'] = ["red","orange"];
hashtable['mellow'] = ["skyblue","yellow","green"];

you can also set and access values in an object using dot notation:

hashtable.screaming = ["red","orange"];
alert(hashtable.screaming[0]);

If you're just looking to keep track of key/value pairs then an object is the way to go:

var hashtable = {};
hashtable['key1'] = 'value1';
hashtable['key2'] = 'value2';
hashtable.key3 = 'value3';
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was just going to type :)
and Exactly what i was looking for. Appreciate your answer a lot.
5

two dimensional array is javascript. That's why you are not getting results on google.

it's something like this.

var arr = [];
arr[0] = [1,12,3,5];
arr[0][0]; // returns 1
arr[0][1]; // returns 12
arr[0][2]; // returns 3
arr[0][3]; // returns 5

or

var outerA = new Array();
outerA[0] = new Array();
outerA[1] = new Array();
outerA[2] = new Array();

2 Comments

I wasnt sure on what is the best to use for my requirement but your answer matches exactly what the question was asked for. Thanks for the answer. But do you mind if i accept Mario Menger's answer as it explains what i was exactly looking for.
yeah!... no worries at all ;) you could just update your question above to fit for the answer you wanted. So that others will not be confused.
0

Although a very late answer , you can use jhashtable js library which almost mimics hashMap datastructure in java/c#.It even has a method toQueryString() which converts the key-value pair to querystring for http requests.

http://www.timdown.co.uk/jshashtable/index.html

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.