JavaScript implements a simple Map sample introduction to _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

/*
* Map object, realize map function
*
Interface
* Size () Gets the number of map elements
* IsEmpty () to determine if the map is empty
* Clear () Delete all elements of map
* Put (key, value) to add elements (key, value) to the map
* Remove (key) deletes the element of the specified key, returns true successfully, and returns false
* GET (key) gets the element value 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 the failure returns null
* ContainsKey (key) to determine if the map contains the elements of the specified key
* Containsvalue (value) determines whether the map contains elements of the specified value
* VALUES () gets an array of all the value in the map (array)
* KEY () 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;
};

To determine if a map is empty
This.isempty = function () {
Return (This.elements.length < 1);
};

Delete all elements of map
This.clear = function () {
this.elements = new Array ();
};

Add an element (key, value) to the map
This.put = function (_key, _value) {
This.elements.push ({
Key: _key,
Value: _value
});
};

Deletes the element of the specified key, returns true successfully, and returns false
This.removebykey = 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;
};

Deletes the element of the specified value, returns true successfully, and returns false
This.removebyvalue = function (_value) {//removebyvalueandkey
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].value = = _value) {
This.elements.splice (i, 1);
return true;
}
}
catch (e) {
bln = false;
}
Return bln;
};

Deletes the element of the specified value, returns true successfully, and returns false
This.removebyvalueandkey = function (_key,_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].value = = _value && This.elements[i].key = = _key) {
This.elements.splice (i, 1);
return true;
}
}
catch (e) {
bln = false;
}
Return bln;
};

Gets the element value 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 false;
}
return false;
};

Gets the element of the specified index (using Element.key,element.value to get the key and value), and the failure returns null
This.element = function (_index) {
if (_index < 0 | | | _index >= this.elements.length) {
return null;
}
return This.elements[_index];
};

Determines whether the map contains elements 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 elements 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;
};

Determines whether the map contains elements of the specified value
This.containsobj = function (_key,_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].value = = _value && This.elements[i].key = = _key) {
bln = true;
}
}
catch (e) {
bln = false;
}
Return bln;
};

Gets 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 the value in the map (array)
This.valuesbykey = function (_key) {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key) {
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;
};

Gets the key through value
This.keysbyvalue = function (_value) {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
if (_value = = This.elements[i].value) {
Arr.push (This.elements[i].key);
}
}
return arr;
};

Gets an array of all keys in the map (array)
This.keysremoveduplicate = function () {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
var flag = true;
for (Var j=0;j<arr.length;j++) {
if (arr[j] = = This.elements[i].key) {
Flag = false;
Break
}
}
if (flag) {
Arr.push (This.elements[i].key);
}
}
return arr;
};
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.