首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
wangchinaking (57)
yjz0065 (113)
coofucoo (105)
Drate (69)
lphpc (30)
smallnest (61)
iiprogram (64)
downmoon (32)
danny_xcz (49)
btbtd (81)
CSDN - 文档中心 - Javascript 阅读:9032   评论: 5    参与评论
标题   用Javascript写的一个映射表类     选择自 yjgx007 的 Blog
关键字   映射表
出处  

该类可以通过关键字(key)查找相对应的值(value),关键字的类型可以是String、Number、Boolean类型,值的类型不限,代码如下:

<script>
function struct(key, value){

  this.key = key;
  this.value = value;

}

function setAt(key, value){
 
  for (var i = 0; i < this.map.length; i++)
  {
    if ( this.map[i].key === key )
    {
      this.map[i].value = value;
      return;
    }
  }
 
  this.map[this.map.length] = new struct(key, value);

}

function lookUp(key)
{
  for (var i = 0; i < this.map.length; i++)
  {
    if ( this.map[i].key === key )
    {
      return this.map[i].value;
    }
  }
 
  return null;
}

function removeKey(key)
{
  var v;
  for (var i = 0; i < this.map.length; i++)
  {
    v = this.map.pop();
    if ( v.key === key )
      continue;
     
    this.map.unshift(v);
  }
}

function getCount(){
  return this.map.length;
}

function isEmpty(){
  return this.map.length <= 0;
}

function classMap() {

  this.map = new Array();

  this.lookUp = lookUp;
  this.setAt = setAt;
  this.removeKey = removeKey;
  this.getCount = getCount;
  this.isEmpty = isEmpty;
}

window.onload = function(){

  var map = new classMap();

  alert("is the map empty? " + map.isEmpty());
 
  // string to array
  map.setAt("sw1", new Array("sw1_1"));
  map.setAt("sw2", new Array("sw2_1", "sw2_2"));
  map.setAt("sw3", new Array("sw3_1", "sw3_2", "sw3_3"));
  alert(map.lookUp("sw5")); // null
  alert(map.lookUp("sw2")); // "sw2_1, sw2_2"
 
  alert(map.getCount()); // 3
 
  // number to string
  map.setAt(1, "sw1");
  map.setAt(2, "sw2");
  alert(map.lookUp(2)); // "sw2"
  map.setAt(2, new Array("sw2_1", "sw2_2"));
  alert(map.lookUp(2)); // "sw2_1, sw2_2"
 
  alert(map.getCount()); // 5
 
  // string to number
  map.setAt("1", 1);
  map.setAt("2", 2);
  alert(map.lookUp("1")); // 1
  alert(map.lookUp(1)); // "sw1"
  map.setAt("sw3", 33);
  alert(map.lookUp("sw3")); // 33
 
  alert(map.getCount()); // 7
 
  // number to number
  map.setAt(1, 11);
  map.setAt(2, 22);
  alert(map.lookUp(1)); // 11
 
  alert(map.getCount()); // 7
 
  map.removeKey(1);
  alert(map.lookUp(1)); // null
 
  alert(map.getCount()); // 6
 
  // boolean to array
  map.setAt(false, new Array("false", "true"));
  alert(map.lookUp(false));
 
  alert(map.getCount()); // 7

}
</script>


相关文章
对该文的评论
rightyeah ( 2005-03-01)
用Dictionary object也可以的
yjgx007 ( 2004-05-19)
to vinsa

但如果是数字的(弱类型,即使是字符串,javascript也认为是分配数组)可就不这么简单了,会出现错误的情况
arr = new Array();
arr["1"] = "abc";
  
arr["5"] = "def";
alert(arr.length);

=======================================
看来数组并不能非常有效的代表我这个映射表
szlen ( 2004-05-14)
阳光商务短信群发王源码版(iSBSMS)  
下载地址:http://www.szlon.com
软件架构:三层结构(表现层+商业逻辑层+数据处理层)
开发语言:C#
数 据 库:Access2000
程序类别:Windows应用程序 
源码价格:680/600元(会员价)

欲购源代码请直接登陆我们的网站下单:http://www.szlen.com

软件运行环境:
   1.本软件使用微软最新的C#语言开发,开发工具包括:Microsoft Visual Studio .NET 2003,Macromedia Dreamweaver MX,Fireworks 4
   2.客户端需要安装.NET Framework SDK 1.1 版.
   3.支持等操作系统:Windows 98, Windows ME, Windows NT,Windows 2000, Windows Server 2003, Windows XP
---------------------------------------

软件名称:阳光商务邮件搜索王源码版(iSBES)  
下载地址:http://www.szlon.com
软件架构:三层结构(表现层+商业逻辑层+数据处理层)
开发语言:C#
数 据 库:Access2000
程序类别:Windows应用程序 
源码价格:580/500元(会员价)

欲购源代码请直接登陆我们的网站下单:http://www.szlen.com

软件运行环境:
   1.本软件使用微软最新的C#语言开发,开发工具包括:Microsoft Visual Studio .NET 2003,Macromedia Dreamweaver MX,Fireworks 4
   2.客户端需要安装.NET Framework SDK 1.1 版.
   3.支持等操作系统:Windows 98, Windows ME, Windows NT,Windows 2000, Windows Server 2003, Windows XP
---------------------------------------
技术支持
QQ:17506793
MSN:sun188com@hotmail.com
网站地址:http://www.szlen.com
联 系 人:陈先生
联系电话:0755-26742591 
传真号码:0755-26742783
电子邮件:sun188com@hotmail.com
软件演示:http://www.szlon.com
---------------------------------------
yjgx007 ( 2004-05-10)
to vinsa
真是这样的,多谢提示!
vinsa ( 2004-05-09)
有这么复杂吗?
javascript直接可以

var a = new Array();
a['haha'] = 'haha,string key!';
a['hoho'] = 'hoho,string key!';

alert(a['haha']);