• js中创建Map集合


    function Map() { //创建一个map集合

    //这里定义数据结构
    var struct = function(key, value) {  
    this.key = key;
    this.value = value;
    }

    //这是添加的方法

    var 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);
    }

    //获取的方法

    var 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;
    }
    //这种是根据自己的需求来定义的方法
    var getIndex = function(index){
    if(this.arr.length-1>=index){
    return this.arr[index].key;
    }
    return null;
    }

    //删除的方法

    var remove = function(key) {
    var v;
    for (var i = 0; i < this.arr.length; i++) {
    v = this.arr.pop();
    if ( v.key*1 === key*1 ) {
    continue;
    }
    this.arr.unshift(v);
    }
    }

    //获取存储的数量

    var size = function() {
    return this.arr.length;
    }

    //判断是否为空

    var isEmpty = function() {
    return this.arr.length <= 0;
    }
    //清空
    var toNull=function(){
    this.arr=new Array();
    }

    //发布服务

    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.toNull=toNull;
    this.remove = remove;
    this.getIndex =getIndex;
    this.getValue=getValue;
    this.size = size;
    this.isEmpty = isEmpty;
    }

  • 相关阅读:
    Eclipse 开发过程中利用 JavaRebel 提高效率
    数字转化为大写中文
    网页变灰
    解决QQ截图无法在PS中粘贴
    ORACLE操作表时”资源正忙,需指定nowait"的解锁方法
    网页常用代码
    SQL Server 2000 删除注册的服务器
    GridView 显示序号
    读取Excel数据到DataTable
    清除SVN版本控制
  • 原文地址:https://www.cnblogs.com/xielinjiang/p/9146220.html
Copyright © 2020-2023  润新知