之前做EXT的用的一个JS的MAP记录一下

这篇博客介绍了如何在JavaScript中实现一个简单的Map数据结构,包括put、get、getValue、getKey、remove、size、isEmpty等操作。通过一个名为`Ext.Map`的命名空间,定义了一个struct构造函数以及一系列与Map相关的函数,用于存储和管理键值对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * javascript map
 */
Ext.namespace("Ext.Map");

function struct(key, value) {
 this.key = key;
 this.value = value;
}

function put(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);
}

function get(key) {
 for (var i = 0; i < this.arr.length; i++) {
  if (this.arr[i].key === key) {
   return this.arr[i].value;
  }
 }
 return null;
}

function getValue(index) {
 return this.arr[index].value;
}

function getKey(index) {
 return this.arr[index].key;
}

function remove(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);
 }
}

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

function isEmpty() {
 return this.arr.length <= 0;
}

function Map() {
 this.arr = new Array();
 this.get = get;
 this.getKey = getKey;
 this.getValue = getValue;
 this.put = put;
 this.remove = remove;
 this.size = size;
 this.isEmpty = isEmpty;
 this.sort = sort;
}
function sort() {
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值