码迷,mamicode.com
首页 > Web开发 > 详细

js 构建map 和list

时间:2017-04-15 12:05:35      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:values   i++   pre   实现   code   ++   value   get   ons   

//构建map
function Map() {  
    this.arr = new Array();  
    var struct = function(key, value) {  
            this.key = key;  
            this.value = value;  
    };
    this.keySet = function() {
        var arrKeys = new Array();
        for(var i = 0; i < this.arr.length; i++) {
            arrKeys.push(this.arr[i].key);
        }
        return arrKeys;
    };
    this.put = function(key, value){  
          for (var i = 0; i < this.arr.length; i++) {  
              if ( this.arr[i].key === key ) {  
                     this.arr[i].value = value;  
                    return;  
               }  
          }  
       this.arr[this.arr.length] = new struct(key, value);  
    };  
    this.get = function(key) {  
       for (var i = 0; i < this.arr.length; i++) {  
          if ( this.arr[i].key === key ) {  
               return this.arr[i].value;  
            }  
        }  
      return null;  
     };  
     this.values=function(){  
        var value=[]  
       for (var i = 0; i < this.arr.length; i++) {  
          value.push(this.arr[i].value);  
       }  
       return value.join(",");  
     };  
     this.remove = function(key) {  
       var v;  
       for (var i = 0; i < this.arr.length; i++) {  
          v = this.arr.pop();  
          if ( v.key === key ) {  
           continue;  
       }  
       this.arr.unshift(v);  
      }  
     };  
     this.size = function() {  
      return this.arr.length;  
     };  
     this.isEmpty = function() {  
         return this.arr.length <= 0;  
     };  
} 

/**  
 * js实现list  
 *   
 */  
function List() {  
    this.value = [];  
  
    /* 添加 */  
    this.add = function(obj) {  
        return this.value.push(obj);  
    };  
  
    /* 大小 */  
    this.size = function() {  
        return this.value.length;  
    };  
  
    /* 返回指定索引的值 */  
    this.get = function(index) {  
        return this.value[index];  
    }
    /*返回指定对象的索引
     */
    this.indexOf = function(obj) {
        for ( var i in this.value) { 
            
            if (obj == this.value[i]) {  
                return i;  
            } 
        }  
    };
    /* 删除指定索引的值 */  
    this.remove = function(index) {  
        this.value.splice(index,1);  
        return this.value;  
    };  
  
    /* 删除全部值 */  
    this.removeAll = function() {  
        return this.value = [];  
    };  
  
    /* 是否包含某个对象 */  
    this.constains = function(obj) {  
        for ( var i in this.value) {  
            if (obj == this.value[i]) {  
                return true;  
            } else {  
                continue;  
            }  
        }  
        return false;  
    };  
      
    /* 是否包含某个对象 */  
    this.getAll = function() {  
        var allInfos = ‘‘;  
        for ( var i in this.value) {  
            if(i != (value.length-1)){  
                allInfos += this.value[i]+",";  
            }else{  
                allInfos += this.value[i];  
            }  
        }  
        alert(allInfos);  
        return allInfos += this.value[i]+",";;  
    };  
      
} 

 

js 构建map 和list

标签:values   i++   pre   实现   code   ++   value   get   ons   

原文地址:http://www.cnblogs.com/edison20161121/p/6713361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!