BKJIA recommended topics: Node. js Zone
This article introduces the Node. js extension module Hashish. Hashish is a JavaScript Hash combination sub-database. In other words, it contains a series of data structure operation functions.
For example:
- var Hash = require('hashish');
-
- Hash({ a : 1, b : 2, c : 3, d : 4 })
- .map(function (x) { return x * 10 })
- .filter(function (x) { return x < 30 })
- .forEach(function (x, key) {
- console.log(key + ' => ' + x);
- })
- ;
Here, the Hash structure is {a: 1, B: 2, c: 3, d: 4}. Next, map a function, and multiply each Hash value by 10, at this time, Hash is transformed into {a: 10, B: 20, c: 30, d: 40}. Then, filter out the values lower than 30. At this time, Hash is {: 10, B: 20}. Finally, forEach will operate on each practical value, and the output value is:
- a => 10
- b => 20
Note that Hash can manipulate each node and all the combinations can be combined to keep your code perfect.
If you cannot or do not want to use the connection function, hashish can also attach each link to the Hash. In this form:
- var Hash = require('hashish');
- var obj = { a : 1, b : 2, c : 3, d : 4 };
-
- var mapped = Hash.map(obj, function (x) {
- return x * 10
- });
-
- console.dir(mapped);
Note that the Code on obj hash is called Hash. map. Multiply the Hash output value by 10:
{A: 10, B: 20, c: 30, d: 40}
In the Hash. xxx link, Hashish also provides various attributes of interfaces and functions. For example:
- $ node
- > var Hash = require('hashish');
- > var obj = { a : 1, b : 2, c : 3, d : 4 };
- >
- > Hash(obj).keys
- [ 'a', 'b', 'c', 'd' ]
- > Hash(obj).values
- [ 1, 2, 3, 4 ]
- > Hash(obj).length
- 4
Of course, you can install hashish through npm:
- npm install hashish
This article introduces Hashish, the Node. js extension module. Do you understand?