java和C#等高级语言中都有map这样的键值对,但是js里没有,我们需要这样的,该怎么做呢?
可以自己使用function封装一个map对象,如下所示

function Map() { this.keys = new Array(); this.data = new Array(); //添加键值对 this.set = function (key, value) { var index = this.keys.indexOf(key); if (index == -1) {//如键不存在则身【键】数组添加键名 this.keys.push(key); this.data.push(value) }else{ this.data[index] = value; } }; //获取键对应的值 this.get = function (key) { var index = this.keys.indexOf(key); return this.data[index]; }; //去除键值,(去除键数据中的键名及对应的值) this.remove = function (key) { var index = this.keys.indexOf(key); if(index >= 0){ this.keys.splice(index); this.data.splice(index); } }; //判断键值元素是否为空 this.isEmpty = function () { return this.keys.length == 0; }; //获取键值元素大小 this.size = function () { return this.keys.length; }; } function test(){ var map = new Map(); map.set('1000001','test'); map.set('1000001','test2'); alert(map.get('1000001')); }
文章来源
http://www.cnblogs.com/xienb/archive/2013/01/04/2843966.html
但是我感觉这个不符合我的运用,并且在运用过程中发现有点问题,所以转载过来修改一下,自己记录一下。