Constructor
new Dictionary()
In computer science, an associative array, map, symbol table, or
dictionary is an abstract data type composed of a collection of (key, value) pairs,
such that each possible key appears at most once in the collection. Full wikipedia article at:
https://en.wikipedia.org/wiki/Associative_array
- Source:
Example
const Dictionary = require('dstructures').Dictionary;
const myDictionary = new Dictionary();
Methods
add(key, value)
Adds key value pair in a dictionary.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
any
|
Given key. |
value |
any
|
Given value. |
Example
Dictionary.add('dog', 1); // ['dog': 1]
Dictionary.add('dog', 2); // ['dog': 2]
Dictionary.add('cat', 1); // ['dog': 2, 'cat': 1]
clear()
Deletes all key value pairs.
- Source:
Example
Dictionary.display(); // ['dog': 2, 'cat': 1]
Dictionary.clear(); // []
count() → {Number}
Counts all key value pairs.
- Source:
Returns:
- Type:
-
Number
Returns number of all key value pairs.
Example
Dictionary.display(); // ['dog': 2, 'cat': 1]
Dictionary.count(); // 2
Dictionary.display(); // ['dog': 2, 'cat': 1, 'pig': 4]
Dictionary.count(); // 3
display() → {Array}
Returns the underlying array.
- Source:
Returns:
- Type:
-
Array
Returns the underlying array.
Example
Dictionary.add('dog', 2); // ['dog': 2]
Dictionary.add('cat', 1); // ['dog': 2, 'cat': 1]
Dictionary.display(); // ['dog': 2, 'cat': 1]
find(key)
Returns the value for the given key.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
any
|
Given key. |
Returns:
Returns the value for the given key.
Example
Dictionary.add('dog', 1); // ['dog': 1]
Dictionary.find('dog'); // 1
Dictionary.add('cat', 3); // ['dog': 1, 'cat': 3]
Dictionary.find('cat'); // 3
remove(key)
Removes the value associated to the given key.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
any
|
Given key. |
Example
Dictionary.add('dog', 2); // ['dog': 2]
Dictionary.add('cat', 1); // ['dog': 2, 'cat': 1]
Dictionary.remove('dog'); // [cat: 1]