Home > Web Front-end > JS Tutorial > Sample code of map method implemented in js_javascript skills

Sample code of map method implemented in js_javascript skills

WBOY
Release: 2016-05-16 17:03:51
Original
1091 people have browsed it
复制代码 代码如下:

/**
*
* Description: map method implemented by js
* @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类同
复制代码 代码如下:


Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template