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]+",";;  
        };  
          
    } 
  • 相关阅读:
    中科大算法分析与设计分布式算法复习知识点
    记录一些实用网站
    《TensorFlow机器学习项目实战》pdf及源码
    DevC++连接MySQL可用详细教程
    【转】MySQL合理使用索引
    【原】基于Feign 重写自定义编码器
    【原】logback实现按业务输出到对应日志文件
    【原】MDC日志链路设计
    关于看源码的心得体会
    【原】基于Spring实现策略模式
  • 原文地址:https://www.cnblogs.com/edison20161121/p/6713361.html
Copyright © 2011-2022 走看看