2

I need a sort of dictionary in my class (I'm not sure has this kind of object is called), I'm using this:

inputCollection: { [name: string]: HTMLInputElement };

I can get and set values like so:

this.inputCollection['inputId'] = document.getElementById('inputId');

I would like to access my dictionary in the following manner, to get the first element

this.inputCollection[0]

How could I achieve this easily ?

2
  • This would be dangerous. Remember TypeScript is just a superset of JavaScript. Your [0] can easily be automatically converted into a string as a key, or stays numeric for offset. Better to make something like an elementAt(offset) method to do the offsetting. Also, why are you getting the first element of a dictionary? By definition, a dictionary shouldn't have any order... Are you trying to enumerate all the elements in the dictionary? In that case, you probably should be using a for in loop. Commented Jun 29, 2013 at 14:00
  • Moral of the story is: TypeScript is not C#. Commented Jun 29, 2013 at 14:01

3 Answers 3

6

If you want to obtain items using either a string or an index, you'll have to implement a Dictionary. There is no framework class library for TypeScript, so you don't get this for free - just the language features to make one.

You can technically use both string and number indexers, but the first string item is not the same as the first number item. Example:

class Item {
    constructor(public name: string) {
    }
}

var items = [];

items['a'] = new Item('One');
items['b'] = new Item('Two');
items[0] = new Item('Three');

alert(items['a'].name); // One
alert(items[0].name); // Three

Here is a really basic Dictionary class.

class Item {
    constructor(public name: string) {
    }
}

class Dictionary<T> {
    private items = [];

    add(key: string, value: T) {
        this.items.push(value);
        this.items[key] = value;
    }

    getByIndex(index: number) {
        return this.items[index];
    }

    getByKey(key: string) {
        return this.items[key];
    }
}

var dictionary = new Dictionary<Item>();
dictionary.add('a', new Item('One'));
dictionary.add('b', new Item('Two'));

alert(dictionary.getByIndex(0).name);
alert(dictionary.getByKey('a').name);

This also works if you change an item...

dictionary.add('a', new Item('One'));

dictionary.getByIndex(0).name = 'Changed';

alert(dictionary.getByIndex(0).name);
alert(dictionary.getByKey('a').name);
Sign up to request clarification or add additional context in comments.

Comments

3

Take a squizzy at basarat's TypeScript Collections lib

If you are working on 0.9.0 you will need to make a few minor fixes but it is good.

1 Comment

Thanks for pointing that out +1 :) . The minor fixes you mention, would that be the fact that I forgot to export an interface used by a public member. Does this fix it: github.com/basarat/typescript-collections/blob/gh-pages/…
1

There is a difference between arrays an objects (what you are using). Object properties are not indexed. i.e. The first element you add is not necessarily in the first memory location. So you cannot do that. If you want such a functionality, use an array instead of dictionary object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.