var x : IHash = {};
x['key1'] = 'valeur1';
x['key2'] = 'valeur2';
console.log(x['key1']);
// outputs valeur1
console.log(x['key2']);
// outputs valeur2
If you would like to then iterate through your dictionary, you can use.
Object.keys(x).forEach((key) => {console.log(x[key])});
Object.keys returns all the properties of an object, so it works nicely for returning all the values from dictionary styled objects.
You also mentioned a hashmap in your question, the above definition is for a dictionary style interface. Therefore the keys will be unique, but the values will not.
You could use it like a hashset by just assigning the same value to the key and its value.
if you wanted the keys to be unique and with potentially different values, then you just have to check if the key exists on the object before adding to it.
var valueToAdd = 'one';
if(!x[valueToAdd])
x[valueToAdd] = valueToAdd;
or you could build your own class to act as a hashset of sorts.
Class HashSet{
private var keys: IHash = {};
private var values: string[] = [];
public Add(key: string){
if(!keys[key]){
values.push(key);
keys[key] = key;
}
}
public GetValues(){
// slicing the array will return it by value so users cannot accidentally
// start playing around with your array
return values.slice();
}
}