Home >Web Front-end >JS Tutorial >Simple implementation code of JS Map and List_javascript skills

Simple implementation code of JS Map and List_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:29:451021browse
Copy code The code is as follows:

/*
* MAP object, implements MAP function
*
* Interface:
* size() Get MAP element Number of items
* isEmpty() Determine whether MAP is empty
* clear() Delete all elements of MAP
* put(key, value) Add elements (key, value) to MAP
* remove (key) Deletes the element of the specified KEY, returns True if successful, False
on failure * get(key) Gets the element value VALUE of the specified KEY, returns NULL
on failure * element(index) Gets the element of the specified index (use element.key, element.value obtains KEY and VALUE), failure returns NULL
* containsKey(key) Determines whether the MAP contains the element with the specified KEY
* containsValue(value) Determines whether the MAP contains the element with the specified VALUE
* values() Get the array of all VALUEs in the MAP (ARRAY)
* keys() Get the array of all the 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() {
                                                                                                  using             using using   using  .       using using           out out through  through Through out off out through off off      through through’’'' through through‐'' ‐‐‐'‐‐‐‐ responsible for to be to go to,                                                                                                          ;
};
//Delete all elements of MAP
this.clear = function() {
this.elements = new Array();
};
//Add to MAP Add elements (key, value)
this.put = function(_key, _value) {
this.elements.push( {
key : _key,
value : _value
}) ;
};
//Delete the element of the specified KEY, return True if successful, False if failed
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 ;                                                                                                                                                                                                                      , //Get the element value VALUE of the specified KEY, and return NULL
on failure this.get = function(_key) {
{
                                                                                                                                                                                                    > } catch (e) {
                                                                                                                                                                                                                                                                                                    ; (_index) {
        if (_index < 0 || _index >= this.elements.length) {
                                                                                                                                      };
//Determine whether the MAP contains the element of the specified KEY
this.containsKey = function(_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
                                                                                                                                    }
} catch (e) {
               bln = false;                                             ;
//Determine whether the MAP contains the element of the specified VALUE
this.containsValue = function(_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
                                                                                                                                                    }
} catch ( e) { bln = false;
}
Return bln;
};
// Get the array of all values ​​in the map (🎜> this.values ​​= function () {
var arr = new Array();
for (i = 0; i < this.elements.length; i ) {
arr.push(this.elements[i].value);
}
}; return arr;
};
//Get the array (ARRAY) of all KEYs in MAP
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
The code is as follows:

/**
* js implementation list
*
*/
function List() {
this.value = [];
/* Add*/
this.add = function(obj ) {
                                                                                            using                       using using   using using   using   using       through out through   out through off off ’ s ’ through ‐ to ‐ ‐′‐‐‐‐ ‐ to. 🎜> };
/* Return the value at the specified index*/
this.get = function(index) {
}; return this.value[index];
};
/* Delete Specify the value of the index*/
this.remove = function(index) {
this.value.splice(index,1);
return this.value;
};
/* Delete all values*/
this.removeAll = function() {
return this.value = [];
};
/* Whether to contain an object*/
this.constains = function(obj) {
for ( var i in this.value) {
if (obj == this.value[i]) {
return true;
continue;
                                                                                                                                                                                 var allInfos = '; ;
                                                                                          Return allInfos = this.value[i] "," ;;
};

}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn