您好,登錄后才能下訂單哦!
js中沒有map這個類,只能自己寫一個。以下map.js和map-util.js都是自定義的map,任選其一就可以。你可以用它來像java里new Map()和put()、remove()、get()等方法。
map.js:
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; }
map-util.js:
function Map() { this.elements = new Array(); var i; //獲取MAP元素個數(shù) this.size = function() { return this.elements.length; }; //判斷MAP是否為空 this.isEmpty = function() { return (this.elements.length < 1); }; //刪除MAP所有元素 this.clear = function() { this.elements = new Array(); }; //向MAP中增加元素(key, value) this.put = function(_key, _value) { this.elements.push( { key : _key, value : _value }); }; this.putFirst = function(_key, _value){ var tempList = this.elements; this.elements = new Array(); this.elements.push( { key : _key, value : _value }); for(var i=0;i<tempList.length;i++){ this.elements.push( tempList[i] ); } } //刪除指定KEY的元素,成功返回True,失敗返回False this.remove = function(_key) { var bln = false; try { for (i = 0; i < this.elements.length; i++) { if (this.elements[i].key == _key) { this.elements.splice(i, 1); return true; } } } catch (e) { bln = false; } return bln; }; //獲取指定KEY的元素值VALUE,失敗返回NULL this.get = function(_key) { try { for (i = 0; i < this.elements.length; i++) { if (this.elements[i].key == _key) { return this.elements[i].value; } } } catch (e) { return null; } }; //獲取指定索引的元素(使用element.key,element.value獲取KEY和VALUE),失敗返回NULL this.element = function(_index) { if (_index < 0 || _index >= this.elements.length) { return null; } return this.elements[_index]; }; //判斷MAP中是否含有指定KEY的元素 this.containsKey = function(_key) { var bln = false; try { for (i = 0; i < this.elements.length; i++) { if (this.elements[i].key == _key) { bln = true; } } } catch (e) { bln = false; } return bln; }; //判斷MAP中是否含有指定VALUE的元素 this.containsValue = function(_value) { var bln = false; try { for (i = 0; i < this.elements.length; i++) { if (this.elements[i].value == _value) { bln = true; } } } catch (e) { bln = false; } return bln; }; //獲取MAP中所有VALUE的數(shù)組(ARRAY) this.values = function() { var arr = new Array(); for (i = 0; i < this.elements.length; i++) { arr.push(this.elements[i].value); } return arr; }; //獲取MAP中所有KEY的數(shù)組(ARRAY) this.keys = function() { var arr = new Array(); for (i = 0; i < this.elements.length; i++) { arr.push(this.elements[i].key); } return arr; }; }
總結
以上所述是小編給大家介紹的 Javacript中自定義的map.js 的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。