function map () {//Create a map collection
This defines the data structure
var struct = function (key, value) {
This.key = key;
This.value = value;
}
This is the method of adding
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);
}
Method of getting
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;
}
This is a method that is defined according to your own needs.
var getindex = function (index) {
if (This.arr.length-1>=index) {
return this.arr[index].key;
}
return null;
}
Method of deletion
var remove = function (key) {
var V;
for (var i = 0; i < this.arr.length; i++) {
v = this.arr.pop ();
if (v.key*1 = = = key*1) {
Continue
}
This.arr.unshift (v);
}
}
Get the number of stores
var size = function () {
return this.arr.length;
}
Determines whether the empty
var isEmpty = function () {
return this.arr.length <= 0;
}
Empty
var tonull=function () {
This.arr=new Array ();
}
Publishing services
This.arr = new Array ();
This.get = get;
This.put = put;
This.tonull=tonull;
This.remove = remove;
This.getindex =getindex;
This.getvalue=getvalue;
this.size = size;
This.isempty = IsEmpty;
}
Create a Map collection in JS