JS Custom Map

Source: Internet
Author: User

function Map () {
this.elements = new Array ();
Get the size of the map
This.size = function () {
return this.elements.length;
}
Determines whether the empty
This.isempty = function () {
Return (This.elements.length < 1);
}
Empty
This.clear = function () {
this.elements = new Array ();
}
Put in a Map object
This.put = function (_key, _value) {
This.elements.push ({
Key: _key,
Value: _value
});
}
Remove an object by key
This.remove = function (_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key && typeof This.elements[i].key = = typeof _key) {
This.elements.splice (i, 1);
return true;
}
}
} catch (e) {
bln = false;
}
Return bln;
}
Gets an object based on the key
This.get = function (_key) {
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key && typeof This.elements[i].key = = typeof _key) {
return this.elements[i].value;
}
}
} catch (e) {
return null;
}
}
Returns an object of the specified index
This.element = function (_index) {
if (_index < 0 | | _index >= this.elements.length) {
return null;
}
return This.elements[_index];
}
Whether the key is included
This.containskey = function (_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].key = = _key && typeof This.elements[i].key = = typeof _key) {
bln = true;
}
}
} catch (e) {
bln = false;
}
Return bln;
}
Whether the value is included
This.containsvalue = function (_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i++) {
if (This.elements[i].value = = _value && typeof this.elements[i].value = = typeof _value) {
bln = true;
}
}
} catch (e) {
bln = false;
}
Return bln;
}
Get all the values
This.values = function () {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
Arr.push (This.elements[i].value);
}
return arr;
}
Get all the keys
This.keys = function () {
var arr = new Array ();
for (i = 0; i < this.elements.length; i++) {
Arr.push (This.elements[i].key);
}
return arr;
}
}

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.