JavaScript implements a simple Map sample introduction

Source: Internet
Author: User

  This article introduces the use of JavaScript to achieve a simple map, you can get the map, judge, delete, add, and so on, interested friends can understand the next

  Code as follows:/*  * Map object, realize map function   *  * Interface:  * size () Get the number of map elements   * IsEmpty () to determine if map is null   * CLE AR () Delete all elements of map   * PUT (key, value) to add element (key, value)   * Remove (key) to map to delete the element of the specified key, return true successfully, fail return false  * GE T (key) gets the element value value of the specified key, and the failure returns null  * Element (index) to get the element of the specified index (using Element.key,element.value to get key and value), Failure returns null   * ContainsKey (key) determines whether the map contains elements of the specified key   * Containsvalue (value) to determine if the map contains elements of the specified value   * VALUES () Gets the array (array)   * keys () of all the value in the map to get an array of all keys in the map (array)   *  * Example:  * var map = new map ();  *&nbsp ; * Map.put ("key", "value");  * var val = map.get ("key")   * ......  *  */  function map () {  this. elements = new Array ();   //Get the number of map elements   this.size = function () {  return this.elements.length; } ;   //Determine if map is empty   This.isempty = function () {  return (This.elements.length < 1); };  &N Bsp Delete all elements of map   This.clear = functIon () {  this.elements = new Array (); };   //add element (key, value)   This.put = function to map (_key, _ Value) {  This.elements.push ({  key: _key,  value: _value }); };   //delete the element of the specified key into Work returns TRUE, Failure returns false  This.removebykey = function (_key) {  var bln = false;  Try {  for (i = 0; i < th Is.elements.length; i++) {  if (This.elements[i].key = = _key {  this.elements.splice (i, 1);  return true; } }&nbsp ; catch (e) {  bln = false; }  return bln; };   //delete element of specified value, returns true successfully, failure returns false  T His.removebyvalue = function (_value) {//removebyvalueandkey  var bln = false;  Try {  for (i = 0; i < th Is.elements.length; i++) {  if (This.elements[i].value = = _value {  this.elements.splice (i, 1);  return true; } }& nbsp catch (e) {  bln = false; }  return bln; };   //delete element of specified value, successfulReturns TRUE, Failure returns false  This.removebyvalueandkey = function (_key,_value) {  var bln = false;  Try {  for (i = 0; i < this.elements.length; i++) {  if (This.elements[i].value = = _value && This.elements[i].key = _key) {  This.elements.splice (i , 1);  return true; } } } catch (e) {  bln = false; }  return bln; };  & nbsp Gets the element value value of the specified key, and the failure returns null  This.get = function (_key) {  try {  for (i = 0; i < this.elements.length i + +) {  if (This.elements[i].key = = _key) {  return this.elements[i].value; } } } catch (e) {&N Bsp Return false; }  return false; };   // Gets the element of the specified index (using Element.key,element.value to get key and value), and the failure returns null  this.element = function (_index) {  if (_index < 0 | | _index >= this.elements.length) {  return null; }  return this.elements[_index]; };   //Determine if the map contains the element   this for the specified key.ContainsKey = function (_key) {  var bln = false;  Try {  for (i = 0; i < this.elements.length; i++) {&NB Sp if (This.elements[i].key = = _key) {  bln = true; } } } catch (e) {  bln = false; }  return bln; };   //Determine if the map contains elements of the specified value   This.containsvalue = function (_value) {  var bln = FA lse;  try {  for (i = 0; i < this.elements.length i++) {  if (This.elements[i].value = _value) {  bln = true; } } } catch (e) {  bln = false; }  return bln; };   //Judge Ma P contains the element with the specified value   this.containsobj = function (_key,_value) {  var bln = false;  Try {  for (i = 0; I & Lt This.elements.length; i++) {  if (This.elements[i].value = = _value && This.elements[i].key = _key) {  bln = true; }&nbs P } } catch (e) {  bln = false; }  return bln; };   //Get an array of all the value in the map (array  this.values = function () {  var arr = new Array ();  for (i = 0; i < this.elements.length; i++) {&nbsp ; Arr.push (this.elements[i].value); }  return arr; };   //Get an array of all the value in the map (array)   This.valuesbykey = function (_key) {  var arr = new Array ();  for (i = 0; i < this.elements.length; i++) {&nbs P if (This.elements[i].key = = _key) {  arr.push (this.elements[i].value); } }  return arr; };& nbsp  //Get the Array (array)   This.keys = function () {  var arr = new Array ();  for all keys in the map (i = 0; i < THIS.E Lements.length; i++) {  Arr.push (this.elements[i].key); }  return arr; };   //Get key through value  This.keysbyvalue = function (_value) {  var arr = new Array ();  for (i = 0; i < this.elements.length; i++) {&N Bsp if (_value = this.elements[i].value) {  arr.push (this.elements[i].key); } }  return arr; };    //Get the Array (array)   this.keysremoveduplicate = function () {  var arr = new Array ();  for all keys in the map (i = 0; i < This.elements.length; i++) {  var flag = true;  for (var j=0;j<arr.length;j++) {  if (arr[j] = = THIS.ELEMENTS[I].KEY) {  fla g = false;  break; } }  if (flag) {  Arr.push (this.elements[i].key); } }  return arr; }; }   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.