Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is there a hashmap in javascript?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces whether there is hashmap in javascript. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

There is hashmap in javascript, and the way to implement hashmap is "function HashMap () {this.map = {};} HashMap.prototype = {put: function...}".

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer

Implementation of HashMap in JavaScript

What is HashMap?

Implementation of Map interface based on hash table. This implementation provides all optional mapping operations and allows the use of null values and null keys. The HashMap class is roughly the same as Hashtable except that it is out of sync and allows the use of null. This class does not guarantee the order of mappings, especially since it does not guarantee that the order is permanent This implementation assumes that the hash function distributes elements appropriately between buckets, providing stable performance for basic operations (get and put). The time required to iterate through the collection view is proportional to the "capacity" (number of buckets) and its size (key-value mapping correlation factor) of the HashMap instance.

Therefore, if iterative performance is important, do not set the initial capacity too high (or set the load factor too low).

Implementation of HashMap in JavaScript var emojMap = [smiley face], "[smile]", "[like]", "[blowing kisses]", "[screaming]", "[crying]", "[laughing and crying]", "[sunglasses]", "[hungry]", "[dazed]", "[contemplation]", "[disdain]", "[grimace]", "[complacency]" "[angry]", "[blink]", "[sweat]", "[comfortable]", "[bad]", "[open mouth]", "[mask]", "[no mouth]", "[devil]", "[sleep]", "[sleepy]", "[uncomfortable]", "[naughty]", "[stubborn]", "[confused]" "[Angel]", "[do not see]", "[do not listen]", "[do not say]", "[pray]", "[scissors hand]", "[fists]", "[upstairs]", "[good]", "[like]", "[contempt]", "[applause]", "[Star]", "[Heart]", "[heartbreak]" "[full marks]", "[purse]", "[poop]", "[ghost]", "[eyes]", "[nose]", "[ears]", "[mouth]", "tongue", "[pig]", "[dog]", "[monkey]", "[pony]", "[panda]", "[bear]" "[alien]"]

This number assembles the Emoji identification data agreed in our development process and sends it to the server side.

Eg: I'm HelloWord! [smile] format

We need to parse the data in the format "[XX]" to match the corresponding picture.

Common methods of HashMap

In view of the operation of HashMap, we need to encapsulate the common operation methods.

Function HashMap () {this.map = {};} HashMap.prototype = {put: function (key, value) {/ / add elements (key, value) this.map [key] = value to Map }, get: function (key) {/ / get the element value Value of the specified Key. Failure returns Null if (this.map.hasOwnProperty (key)) {return this.map [key];} return null }, remove: function (key) {/ / Delete the element of the specified Key, return True for success, return False if (this.map.hasOwnProperty (key)) {return delete this.map [key];} return false;}, removeAll: function () {/ / clear all elements of HashMap this.map = {} }, keySet: function () {/ / get the array of all KEY in Map (Array) var _ keys = []; for (var i in this.map) {_ keys.push (I);} return _ keys;}; HashMap.prototype.constructor = HashMap

The above is the HashMap operation method we encapsulated.

Using HashMap to develop Emoji expression Library

At first I thought of several options, just like the emojiMap array, if the other party sent a message

Eg: I'm HelloWord! [smile] format

Var r = /\ [(. +?)\] / g; var str = "[smiley face] [like] emoji expression"; var txt,url,tpl; for (var i in str.match (r)) {tpl = "

"; str = str.replace (str.match (r) [I], tpl);} console.log (str)

My initial idea was to use split to split into arrays, and then replace the corresponding images with replace. Later, I found that there would be problems with too many expressions and could not be replaced.

And you need to specify the index location of the array subscript

Later, I changed it to the following method:

Var hashMap = new HashMap (); / / first deposit the element for (var i in emojMap) {hashMap.put (parseInt [I]) + '.png') in hashMap;} var r = / ([^\ []] +) (? =\]) / g; var str = "[smiley face] [like] emoji expression"; var txt,url,tpl For (var i in str.match (r)) {/ / get the corresponding Value txt = hashMap.get (str.match (r) [I]) tpl = "in hashMap.

"; str= str.split (m [I]) .join (tpl);} str=str.replace (/\ [|] / g str'); console.log (join)

The advantage of using HasMap is that you don't have to worry about the location of the key, because each key corresponds to a val.

In this way, it can be perfectly replaced with Emoji image display.

The above is all the contents of the article "is there a hashmap in javascript?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report