There is no map and list structure in JavaScript.
This article is a detailed analysis of the simple implementation code of MAP and list in JS, the need for a friend reference
The code is as follows:
/*
* Map object, implement map function
*
Interface
* Size () Gets the number of map elements
* IsEmpty () to determine if the map is empty
* Clear () Delete all map elements
* Put (key, value) adds an element to the map (key, value)
* Remove (key) deletes the element of the specified key, returns true successfully, and fails to return false
* GET (key) gets the element value of the specified key, and the failure returns null
* Element (index) Gets the elements of the specified index (using Element.key,element.value to get the key and value), and fails to return null
* ContainsKey (key) to determine if the map contains the specified key element
* Containsvalue (value) to determine if the map contains an element of the specified value
* VALUES () gets an array of all value in the map (array)
* Keys () gets an array of all keys in the map (array)
*
Example
* var map = new map ();
*
* Map.put ("key", "value");
* var val = map.get ("key")
* ......
*
*/
function Map () {
this.elements = new Array ();
Get the number of map elements
This.size = function () {
return this.elements.length;
};
Determine if the map is empty
This.isempty = function () {
Return (This.elements.length < 1);
};
Delete all elements of map
This.clear = function () {
this.elements = new Array ();
};
Adding elements to a map (key, value)
This.put = function (_key, _value) {
This.elements.push ({
Key: _key,
Value: _value
});
};
Deletes the element of the specified key, returns true successfully, and fails to return false
This.remove = function (_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key) {
This.elements.splice (i, 1);
return true;
}
}
} catch (e) {
bln = false;
}
Return bln;
};
Gets the element value of the specified key, and the failure returns null
This.get = function (_key) {
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key) {
return this.elements[i].value;
}
}
} catch (e) {
return null;
}
};
Gets the element of the specified index (using Element.key,element.value to get the key and value), and fails to return null
This.element = function (_index) {
if (_index < 0 | | _index >= this.elements.length) {
return null;
}
return This.elements[_index];
};
Determine if the map contains an element of the specified key
This.containskey = function (_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key) {
bln = true;
}
}
} catch (e) {
bln = false;
}
Return bln;
};
Determines whether the map contains an element of the specified value
This.containsvalue = function (_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].value = = _value) {
bln = true;
}
}
} catch (e) {
bln = false;
}
Return bln;
};
Get an array of all the value in the map (array)
This.values = function () {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
Arr.push (This.elements[i].value);
}
return arr;
};
Gets an array of all keys in the map (array)
This.keys = function () {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
Arr.push (This.elements[i].key);
}
return arr;
};
}
Copy CodeThe code is as follows:
/**
* JS Implementation list
*
*/
function List () {
This.value = [];
/* Add */
This.add = function (obj) {
return This.value.push (obj);
};
/* Size */
This.size = function () {
return this.value.length;
};
/* Returns the value of the specified index */
This.get = function (index) {
return This.value[index];
};
/* Delete the value of the specified index */
This.remove = function (index) {
This.value.splice (index,1);
return this.value;
};
/* Delete all values */
This.removeall = function () {
return this.value = [];
};
/* contains an object */
This.constains = function (obj) {
for (var i in This.value) {
if (obj = = This.value[i]) {
return true;
} else {
Continue
}
}
return false;
};
/* contains an object */
This.getall = function () {
var allinfos = ';
for (var i in This.value) {
if (i! = (value.length-1)) {
Allinfos + = this.value[i]+ ",";
}else{
Allinfos + = This.value[i];
}
}
alert (Allinfos);
return Allinfos + = this.value[i]+ ",";
};
}
Simple implementation code for JS Map and List