复制代码 代码如下:
/**
*
* 描述:js实现的map方法
* @returns {Map}
*/
function Map(){
var struct = function(key, value) {
this.key = key;
this.value = value;
};
// 添加map键值对
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);
};
// 根据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;
};
// 根据key删除
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);
}
};
// 获取map键值对个数
var size = function() {
return this.arr.length;
};
// 判断map是否为空
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;
}
使用方法和java中Map类同
复制代码 代码如下:
<script type="text/javascript">
var map=new Map();
map.put("num",1);
map.put("ss","wss");
alert(map.size());
alert(map.get("num"));
paraArr.remove("num");
alert(map.size());
alert(map.get("num"));
</script>
/**
*
* 描述:js实现的map方法
* @returns {Map}
*/
function Map(){
var struct = function(key, value) {
this.key = key;
this.value = value;
};
// 添加map键值对
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);
};
// 根据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;
};
// 根据key删除
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);
}
};
// 获取map键值对个数
var size = function() {
return this.arr.length;
};
// 判断map是否为空
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;
}
使用方法和java中Map类同
复制代码 代码如下:
<script type="text/javascript">
var map=new Map();
map.put("num",1);
map.put("ss","wss");
alert(map.size());
alert(map.get("num"));
paraArr.remove("num");
alert(map.size());
alert(map.get("num"));
</script>
标签:
js,map方法
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“js实现的map方法示例代码”评论...