zoukankan      html  css  js  c++  java
  • js 实现存储Map 结构的数据

    <script type="text/javascript">
    function 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 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);
    }
    }

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

    var isEmpty = function() {
    return this.arr.length <= 0;
    }
    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
    }
    </script>

    ====================================

    <script type="text/javascript">
    var map=new Map();
    map.put('h','hello');
    map.put('hw','world');
    console.log(map);
    </script>

    ====================================

  • 相关阅读:
    rqnoj PID95:多多看DVD(加强版)
    洛谷1309:瑞士轮
    codevs 1052:地鼠游戏
    noi openjudge7627:鸡蛋的硬度
    codevs 1039:数的划分
    lesson 20 pioneer pilots
    TPO-10 C2 Return a literature book
    lesson 19 A very dear cat
    word record 01
    lesson 18 Electric currents in modern art
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6294187.html
Copyright © 2011-2022 走看看