nodejs中使用hashmap
来源:互联网 发布:日本人的生活知乎 编辑:程序博客网 时间:2024/05/06 09:53
本文系转载
原文地址:
https://www.npmjs.com/package/hashmap
hashmap
Installation
Using npm:
$ npm install hashmap
Using bower:
$ bower install hashmap
You can download the last stable version from the releases page.
If you like risk, you can download the latest master version, it's usually stable.
To run the tests:
$ npm test
Description
This project provides a HashMap
class that works both on Node.js and the browser. HashMap instances store key/value pairs allowing keys of any type.
Unlike regular objects, keys will not be stringified. For example numbers and strings won't be mixed, you can passDate
's, RegExp
's, DOM Elements, anything! (even null
and undefined
)
HashMap constructor overloads
new HashMap()
creates an empty hashmapnew HashMap(map:HashMap)
creates a hashmap with the key-value pairs ofmap
new HashMap(key:*, value:*, key2:*, value2:*, ...)
creates a hashmap with several key-value pairs
HashMap methods
get(key:*) : *
returns the value stored for that key.set(key:*, value:*) : HashMap
stores a key-value pairmulti(key:*, value:*, key2:*, value2:*, ...) : HashMap
stores several key-value pairscopy(other:HashMap) : HashMap
copies all key-value pairs from other to this instancehas(key:*) : Boolean
returns whether a key is set on the hashmapsearch(value:*) : *
returns key under which given value is stored (null
if not found)remove(key:*) : HashMap
deletes a key-value pair by keytype(key:*) : String
returns the data type of the provided key (used internally)keys() : Array<*>
returns an array with all the registered keysvalues() : Array<*>
returns an array with all the valuescount() : Number
returns the amount of key-value pairsclear() : HashMap
removes all the key-value pairs on the hashmapclone() : HashMap
creates a new hashmap with all the key-value pairs of the originalhash(key:*) : String
returns the stringified version of a key (used internally)forEach(function(value, key)) : HashMap
iterates the pairs and calls the function for each one
Method chaining
All methods that don't return something, will return the HashMap instance to enable chaining.
Examples
Assume this for all examples below
var map = new HashMap();
If you're using this within Node, you first need to import the class
var HashMap = require('hashmap').HashMap;
Basic use case
map.set("some_key", "some value");map.get("some_key"); // --> "some value"
No stringification
map.set("1", "string one");map.set(1, "number one");map.get("1"); // --> "string one"
A regular Object
used as a map would yield "number one"
Objects as keys
var key = {};var key2 = {};map.set(key, 123);map.set(key2, 321);map.get(key); // --> 123
A regular Object
used as a map would yield 321
Iterating
map.set(1, "test 1");map.set(2, "test 2");map.set(3, "test 3");map.forEach(function(value, key) { console.log(key + " : " + value);});
Method chaining
map .set(1, "test 1") .set(2, "test 2") .set(3, "test 3") .forEach(function(value, key) { console.log(key + " : " + value); });
- nodejs中使用hashmap
- 【NodeJs】NodeJs中使用highstock
- NodeJS中使用shell
- Nodejs中使用Log4js
- nodejs 中使用 ftp
- nodejs中cluster使用
- nodejs中使用mongodb
- Nodejs中使用Log4js
- Nodejs中Log4js使用
- linux中使用hashmap
- HashMap使用中注意事项
- nodejs Express 中session使用
- nodeJs-express中使用session
- nodejs中Async库使用
- nodejs中mysql的使用
- 在NodeJs中使用jQuery?
- nodeJS中redis初步使用
- nodejs中使用bcrypt加密
- Android自定义统计图(柱状图,折线图,饼状图)
- 语音信号处理—MFCC特征提取(matlab代码)
- 一、嵌入式学习方法
- 关于Android收起输入法时会出现屏幕部分黑屏解决
- mongoDB的基本操作之创建索引
- nodejs中使用hashmap
- 字符数组初始化
- spring security各个版本地址
- UDP编程中的connect
- int和NSInteger的区别
- android手势和触摸事件
- 题目 1010 A + B 九度Online Judge
- Android Http库:Netroid
- 微信公众平台开发者原理结构图入门