This article illustrates the method of JavaScript implementing the Map container in Java. Share to everyone for your reference, specific as follows:
The difference between JavaScript and Java is like that of Lei Feng and Leifeng Pagoda.
In Java, a map is a collection of containers that store Key-value key-value pairs. A value is obtained from the key, so the key is not allowed to repeat (repeated overrides), but the value is allowed to duplicate. The object attribute in JavaScript is that it does not allow the same attribute to exist, and Java map is very similar, so you can use this feature in JavaScript to implement the map container, to achieve basic additions and deletions of the operation.
function Map () {
var obj = new Object ();//Empty object container, load key value pair
//put method
this.put = function (key,value) {
Obj[key] = value; The key value pair binds the Obj object to the
//size method, gets the number of map containers
this.size = function () {
var count;
for (var attr in obj) {
count++
}
return count;
}
Get method, obtain value
This.get = function (key) {
if (Obj[key] | | obj[key] = = 0 | | obj[key] = = False) {
return Obj[key];
} else{return
null;
}
Remove method, according to key to delete key value pair
This.remove = function (key) {
if (Obj[key] | | | obj[key] = = 0 | | obj[key] = = False) {
Delete Obj[key];
}
Traversing the map container element method, Eachmap
this.eachmap = function (fn) {for
(var attr in obj) {
fn (attr,obj[attr));
}
}
More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript traversal algorithm and Skills summary", "JavaScript Mathematical calculation Usage Summary", " JavaScript data structure and algorithm skills summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and techniques summary" and "JavaScript error and debugging skills Summary"
I hope this article will help you with your JavaScript programming.