Home > Article > Web Front-end > JavaScript implements the Map object function in Java (detailed answer, code attached)
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; }; }
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"));
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. 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!