zoukankan      html  css  js  c++  java
  • 自定义JS Map 函数

    // 自定义JS Map 函数
    function Map() {
    var map = function (key, value) {//键值对
    this.key = key;
    this.value = value;
    }
    var put = function (key, value) {//添加键值对

    this.arr[this.arr.length] = new map(key, value);
    }
    var remove = function (key) {//删除key="key"的键值对,返回value值
    for (var i = 0; i < this.arr.length; i++) {
    var temp = this.arr.pop();
    if (this.arr[i].key === key) {

    return this.arr[i].value;
    }
    this.arr.push(temp);
    }
    return null;
    }
    var getKey = function (value) {//返回key对应的value值
    for (var i = 0; i < this.arr.length; i++) {
    if (this.arr[i].value === value)
    return this.arr[i].key;
    }
    return null;
    }
    var getValue = function (key) {//返回value对应的key值
    for (var i = 0; i < this.arr.length; i++) {
    if (this.arr[i].key === key)
    return this.arr[i].value;
    }
    return null;
    }
    var getSize = function () {//返回容器大小
    return this.arr.length;
    }

    var show = function () {//打印容器内容
    var string = "";
    for (var i = 0; i < this.arr.length; i++) {
    string += (this.arr[i].key + ":" + this.arr[i].value + " ");
    }
    alert(string);
    }
    this.arr = new Array();
    this.remove = remove;
    this.put = put;
    this.show = show;
    this.getKey = getKey;
    this.getValue = getValue;
    this.getSize = getSize;

    }

  • 相关阅读:
    《程序员修炼之道+从小工到专家》读后感2
    java在子类中,调用父类中被覆盖的方法
    长按文本全复制
    NSTimer 定时器总结
    对URL编码
    Mysql find_in_set 效率问题
    php 运算符优先级
    使用layui上传控件问题
    xcode11发版一直卡在App Store验证过不去
    iOS13禁用深色模式
  • 原文地址:https://www.cnblogs.com/90nice/p/9524192.html
Copyright © 2011-2022 走看看