zoukankan      html  css  js  c++  java
  • js 构建map 和list

    //构建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]+",";;  
        };  
          
    } 
  • 相关阅读:
    第二百一十五节,jQuery EasyUI,DateBox(日期输入框)组件
    第二百一十四节,jQuery EasyUI,Calendar(日历)组件
    onethink 系统函数中 生成随机加密key
    本地开发 localhost链接数据库比127.0.0.1慢
    仿写thinkphp的I方法
    判断数组中有没有某个键 isset 和 array_key_exists 的效率比较
    jquery实时监听某个文本框的输入事件
    js数组去重
    thinkphp3.2.3 版本使用redis缓存的时候无法使用认证
    javascript中使用md5函数
  • 原文地址:https://www.cnblogs.com/edison20161121/p/6713361.html
Copyright © 2011-2022 走看看