Create Map object - Node.js Object

Node.js examples for Object:Object Operation

Description

Create Map object

Demo Code


function Map(){//from  w w  w .  ja v  a 2 s. c om
  this.keyarr = new Array();
  this.valuearr = new Array();
}

Map.prototype.put=function(key,value){
  this.keyarr.push(key);
  this.valuearr.push(value);
};

Map.prototype.load = function(){
  for(var i=0 ; i<arguments.length ; i++){
    var item = arguments[i];
    if(item){
      var index = item.indexOf("=");
      if(index != -1){
        this.keyarr.push(item.substring(0,index));
        this.valuearr.push(item.substring(index+1));
      }
    }
  }
};

Map.prototype.indexOf = function(key){
  for (var i = 0; i < this.keyarr.length; i++) {
    if (this.keyarr[i] == key) {
      return i;
    }
  }
  return -1;
};

Map.prototype.get = function(key){
  var i = this.indexOf(key);
  if( i != -1){
    return this.valuearr[i];
  }
  return null ;
};

Map.prototype.remove = function(key){
  var i = this.indexOf(key);
  if(i != -1){
    this.keyarr.splice(i , 1);
    this.valuearr.splice(i , 1);
  }
};

Map.prototype.size = function(){
  return this.keyarr.length ;
};

Map.prototype.toString = function(){
  var str = "";
  if(this.keyarr.length > 0)
  {
    for(var i = 0 ; i<this.keyarr.length-1 ; i++){
      var itemstr = this.keyarr[i]+"="+this.valuearr[i]+",";
      str += itemstr;
    }
    str += this.keyarr[this.keyarr.length-1]+"="+this.valuearr[this.keyarr.length-1];
  }
  return str ;
};

Related Tutorials