js實現map操作,js自定義map

我們在spring框架中,經常用到將數據放入model 傳到前端進行使用,但是如果我們直接傳遞一個Map,有時候不能直接使用,比如在標籤中我們可以直接 用

    ${columnType.key}

獲取,但是在script中用的時候,不能直接獲取,假如map 的 key 爲 int類型時候,直接就報錯了

怎麼解決呢?

首先,在這裏講一下

JavaScript prototype 屬性

 

    定義和用法

    prototype上的屬性、方法叫原型屬性、原型方法,會使實例化對象都具有該屬性、方法。

    例如Map.prototype.put = function(){},則以後map實例對象都具有put方法。

語法

object.prototype.name=value

 

然後,開始:(以下的部分代碼爲轉載內容)

    /**
     * js實現的map
     */
    // 定義map  
    function Map() {
        this.container = {};
    }

    // 將key-value放入map中  
    Map.prototype.put = function (key, value) {
        try {
            if (key != null && key != "")
                this.container[key] = value;
        } catch (e) {
            return e;
        }
    };

    // 根據key從map中取出對應的value  
    Map.prototype.get = function (key) {
        try {
            return this.container[key];
        } catch (e) {
            return e;
        }
    };

    // 判斷map中是否包含指定的key  
    Map.prototype.containsKey = function (key) {

        try {
            for (var p in this.container) {
                if (p == key)
                    return true;
            }
            return false;
        } catch (e) {
            return e;
        }
    }

    // 判斷map中是否包含指定的value  
    Map.prototype.containsValue = function (value) {
        try {
            for (var p in this.container) {
                if (this.container[p] === value)
                    return true;
            }
            return false;
        } catch (e) {
            return e;
        }
    };

    // 刪除map中指定的key  
    Map.prototype.remove = function (key) {
        try {
            delete this.container[key];

        } catch (e) {
            return e;
        }
    };

    // 清空map  
    Map.prototype.clear = function () {
        try {
            delete this.container;
            this.container = {};
        } catch (e) {
            return e;
        }
    };

    // 判斷map是否爲空  
    Map.prototype.isEmpty = function () {
        if (this.keyArray().length == 0)
            return true;
        else
            return false;
    };

    // 獲取map的大小  
    Map.prototype.size = function () {
        return this.keyArray().length;
    }

    // 返回map中的key值數組  
    Map.prototype.keyArray = function () {
        var keys = new Array();
        for (var p in this.container) {
            keys.push(p);
        }
        return keys;
    }

    // 返回map中的value值數組  
    Map.prototype.valueArray = function () {
        var values = new Array();
        var keys = this.keyArray();
        for (var i = 0; i < keys.length; i++) {
            values.push(this.container[keys[i]]);
        }
        return values;
    }
    // 創建一個 map
    var dataMap = new Map();

 

然後,調用:

 

dataMap.put("","");
dataMap.get("");

在我的項目中,由於下拉框也用到了所需數據,我直接在

<c:forEach>

 

直接加 <script> 

 

<c:forEach items="${columnTypes}" var="columnType">
    <option value="${columnType}">${columnType.value}</option>
    <script>
        dataMap.put('${columnType.key}', '${columnType.value}');
    </script>
</c:forEach>

由此,結束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章