Javacript中自定義的map.js 的方法
js中沒有map這個(gè)類,只能自己寫一個(gè)。以下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元素個(gè)數(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; }; }
總結(jié)
以上所述是小編給大家介紹的 Javacript中自定義的map.js 的方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
通過sails和阿里大于實(shí)現(xiàn)短信驗(yàn)證
本篇文章主要介紹了通過sails和阿里大于實(shí)現(xiàn)短信驗(yàn)證的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理圖文詳解
這篇文章主要介紹了微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理,結(jié)合實(shí)例形式詳細(xì)分析了微信小程序前臺(tái)數(shù)據(jù)form表單提交及后臺(tái)使用php進(jìn)行處理相關(guān)操作技巧,并配以圖文形式詳細(xì)說明,需要的朋友可以參考下2019-03-03javascript 返回?cái)?shù)組中不重復(fù)的元素
返回?cái)?shù)組中不重復(fù)的元素的js實(shí)現(xiàn)代碼。2009-12-12關(guān)于JavaScript數(shù)組你所不知道的3件事
這篇文章主要為大家詳細(xì)介紹了關(guān)于JavaScript數(shù)組三個(gè)并不那么常見的功能,你所不知道的事情,感興趣的小伙伴們可以參考一下2016-08-08利用JavaScript實(shí)現(xiàn)創(chuàng)建虛擬鍵盤的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)創(chuàng)建虛擬鍵盤,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JavaScript有一定幫助,需要的可以參考一下2022-09-09element-ui的回調(diào)函數(shù)Events的用法詳解
這篇文章主要介紹了element-ui的回調(diào)函數(shù)Events的用法,本文通過實(shí)例代碼給大家介紹了change回調(diào)函數(shù)的使用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10JS實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)贊與踩功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)贊與踩功能,涉及javascript針對(duì)頁(yè)面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-12-12關(guān)于IE、Firefox、Opera頁(yè)面呈現(xiàn)異同 寫腳本很痛苦
關(guān)于IE、Firefox、Opera頁(yè)面呈現(xiàn)異同 寫腳本很痛苦,對(duì)于多瀏覽器的兼容性,需要注意。2009-08-08