javascript实现有序map
javascript实现有序map 示例 用法 代码根据网络代码改编 效率未知
只是根据数组方式记录顺序 需要其他功能自己改写吧 或者有更好办法可以告诉我
代码片.
function HashMap() {
//维护映射 this.map = {
}; //维护顺序 this.arrayLink=[] } HashMap.prototype = {
put: function (key, value) {
// 向Map中增加元素(key, value) var numb= this.arrayLink.indexOf(key) // console.log(numb) if ( numb==-1) {
this.arrayLink.push(key) ; numb= this.arrayLink.indexOf(key) } // console.log(numb) // console.log(this.arrayLink) this.map[numb] = value; }, get: function (key) {
//获取指定Key的元素值Value,失败返回Null var numb= this.arrayLink.indexOf(key) if (this.map.hasOwnProperty(numb)) {
return this.map[numb]; }else {
return null; } }, remove: function (key) {
// 删除指定Key的元素,成功返回True,失败返回False var numb= this.arrayLink.indexOf(key) this.arrayLink.splice(numb, 1, "-1") //喊头不含尾 var arrayLinktt=this.arrayLink.slice(0, numb) var arrayLinktt1=this.arrayLink.slice(numb+1, this.arrayLink.length) this.arrayLink = []; this.arrayLink.concat(arrayLinktt) this.arrayLink.concat(arrayLinktt1) if (this.map.hasOwnProperty(numb)) {
return delete this.map[numb]; } return false; }, removeAll: function () {
//清空HashMap所有元素 this.map = {
}; this.arrayLink = []; }, keySet: function () {
//获取Map中所有KEY的数组(Array) return this.arrayLink; } }; HashMap.prototype.constructor = HashMap; //使用 var ttt=new HashMap() ttt.put(1,"3333") ttt.put(888,"232") //将生成html渲染进html var asddddddd=ttt.keySet() for(var i in asddddddd) {
var tmp= ttt.get(asddddddd[i]); console.log(tmp) }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223709.html原文链接:https://javaforall.net
