Home > Web Front-end > JS Tutorial > body text

JavaScript implements the Map object function in Java (detailed answer, code attached)

亚连
Release: 2018-05-19 09:30:14
Original
1677 people have browsed it

This article mainly introduces the relevant information about javascript custom objects to implement the Map object function in Java. This function is implemented here to help everyone understand this part of the content. Friends in need can refer to it

javascript Custom objects implement the Map object function in Java

There are collection, Map and other object storage tool classes in Java. These objects are easy to use, but in JavaScript, you can only use Array objects. .

Here I create a custom object. This object contains an array to store data. The data object is a Key, which can actually store the content!

For Key here, you have to use the String type. Just like Java, you can perform some operations of adding, deleting, modifying, and obtaining.

It’s very simple to use. Let me show you the tool class first:

/** 
 * @version 1.0 
 * @author cuisuqiang@163.com 
 * 用于实现页面 Map 对象,Key只能是String,对象随意 
 */ 
var Map = function(){ 
 this._entrys = new Array(); 
  
 this.put = function(key, value){ 
  if (key == null || key == undefined) { 
   return; 
  } 
  var index = this._getIndex(key); 
  if (index == -1) { 
   var entry = new Object(); 
   entry.key = key; 
   entry.value = value; 
   this._entrys[this._entrys.length] = entry; 
  }else{ 
   this._entrys[index].value = value; 
  }   
 }; 
 this.get = function(key){ 
  var index = this._getIndex(key); 
  return (index != -1) ? this._entrys[index].value : null; 
 }; 
 this.remove = function(key){ 
  var index = this._getIndex(key); 
  if (index != -1) { 
   this._entrys.splice(index, 1); 
  } 
 }; 
 this.clear = function(){ 
  this._entrys.length = 0;; 
 }; 
 this.contains = function(key){ 
  var index = this._getIndex(key); 
  return (index != -1) ? true : false; 
 }; 
 this.getCount = function(){ 
  return this._entrys.length; 
 }; 
 this.getEntrys = function(){ 
  return this._entrys; 
 }; 
 this._getIndex = function(key){ 
  if (key == null || key == undefined) { 
   return -1; 
  } 
  var _length = this._entrys.length; 
  for (var i = 0; i < _length; i++) { 
   var entry = this._entrys[i]; 
   if (entry == null || entry == undefined) { 
    continue; 
   } 
   if (entry.key === key) {//equal 
    return i; 
   } 
  } 
  return -1; 
 }; 
}
Copy after login

If you don’t understand some basic knowledge such as object creation in Js, you can check it online.

// 自定义Map对象 
var map = new Map(); 
map.put("a","a"); 
alert(map.get("a")); 
map.put("a","b"); 
alert(map.get("a"));
Copy after login

Pop a first and then pop b , because the latter one will overwrite the previous one!

If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help everyone. Thank you for your support of this site!

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Vue.jsAnalysis of steps to configure login form code

##Vue. jsSummary of use of form controls

jsPass json parameters to controller step analysis

The above is the detailed content of JavaScript implements the Map object function in Java (detailed answer, code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template