JavaScript implements the method of map container in Java _javascript tips

Source: Internet
Author: User
Tags delete key javascript array

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.

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.