如何 - 使用 JQuery 在二维数组和哈希表中存储键值对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3394770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To - Store Key Value Pair in Two Dimensional Array and HashTable using JQuery?
提问by Asdfg
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.
有人可以将我重定向到正确的链接或举例说明如何在 JQuery 中使用二维数组或 HashTable 吗?我试过谷歌,但没有得到答案。我想避免使用任何插件。我想要做的就是存储一些信息并像 HashTable 一样检索它们。
回答by Mario Menger
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 pairsthen an object is the way to go:
如果您只是想跟踪键/值对,那么对象就是要走的路:
var hashtable = {};
hashtable['key1'] = 'value1';
hashtable['key2'] = 'value2';
hashtable.key3 = 'value3';
回答by Reigel
two dimensional array is javascript. That's why you are not getting results on google.
二维数组是javascript。这就是为什么你没有在谷歌上得到结果的原因。
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();
回答by cherit
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.
虽然答案很晚,但您可以使用 jhashtable js 库,它几乎模仿了 java/c# 中的 hashMap 数据结构。它甚至有一种toQueryString()
将键值对转换为 http 请求的查询字符串的方法。