JS Map and List of simple implementation code _JAVASCRIPT skills

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.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 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 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;
};
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 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 Code code 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 Full value * *
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]+ ",";
};

}

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.