在高级语言c#中存在 dictinary 这种key : value 的存储结构,但在js中没有这种现成的结构,只有数组这种结构,改编就是对数组的操作,实现 dictionary 的增删改查。看完代码你们可以试着写一个List
/**
* 字典
*/
function dictionary() {
this.count=0;
this.keys = new Array();
this.values = new Array();
}
dictionary.prototype.containKey=function(key){
var index = this.checkKeyId(key);
if(index != -1)
{
return true;
}
return false;
},
dictionary.prototype.add=function(key,value){
var index = this.checkKeyId(key);
if(index != -1)
{
//print(key + " is exit");
//alert(key + " is exit");
console.log(key + " is exit");
return;
}
this.keys[this.count] = key;
this.values[this.count] = value;
++this.count;
// console.log(key+":"+value);
},
dictionary.prototype.remove=function(key){
var index = this.checkKeyId(key);
if(-1==index)
{
console.log(key+" does not exist");
return;
}
this.keys.splice(index,1);
this.values.splice(index,1);
--this.count;
}
dictionary.prototype.get=function(key){
var index = this.checkKeyId(key);
if(-1==index)
{
console.log(key+" does not exist:");
return null;
}
return this.values[index];
}
dictionary.prototype.checkKeyId=function(key){
for(i=0;i<this.count;i++)
{
if(key==this.keys[i])
{
return i;
}
}
return -1;
}
dictionary.prototype.getNameByIndex = function(index)
{
return this.keys[index];
}
module.exports = dictionary;