简体   繁体   English

在 TypeScript 中是否有任何定义的 HashTable 类,如 C#

[英]Is there any defined HashTable class in TypeScript like C#

I am developing web project using TypeScript.我正在使用 TypeScript 开发 Web 项目。 Here i need HashTable functionality in typescript like C# HashTable .在这里,我需要像C# HashTable这样的打字稿中的HashTable功能。 But i have already developed that in JavaScript .但我已经在JavaScript 中开发了它。

        this.length = 0;
    this.items = [];
    this.add = function (key, value) {
        this.previous = undefined;
        if (this.containsKey(key)) {
            this.previous = this.items[key];
        } else {
            this.length++;
        }
        this.items[key] = value;
        return this.previous;
    };
    this.clear = function () {
        this.items = {};
        this.length = 0;
    };
    this.contains = function (key) {
        return this.items.hasOwnProperty(key);
    };
    this.containsKey = function (key) {
        return this.items.hasOwnProperty(key);
    };
    this.containsValue = function (key) {
        return (this.items.hasOwnProperty(key) && this.items[key] != undefined) ? true : false;
    };
    this.getItem = function (key) {
        if (this.containsKey(key))
        {

            return this.items[key]
        }
        else
        {
            return  undefined;
        }
    };
    this.keys = function () {
        var keys = [];
        for (var k in this.items) {
            if (this.containsKey(k)) {
                keys.push(k);
            }
        }
        return keys;
    };
    this.remove = function (key) {
        if (this.containsKey(key)) {
            this.previous = this.items[key];
            this.length--;
            delete this.items[key];
            return this.previous;
        } else {
            return undefined;
        }
    };
    this.values = function () {
        var values = [];
        for (var k in this.items) {
            if (this.containsKey(k)) {
                values.push(this.items[k]);
            }
        }
        return values;
    };
    this.each = function (fn) {
        for (var k in this.items) {
            if (this.containsKey(k)) {
                fn(k, this.items[k]);
            }
        }
    };
    var previous = undefined;
}
return HashTable;

Like this, Typescript having predefined code?像这样,Typescript 有预定义的代码吗? or i need to rewrite these codes from JS to TS?或者我需要将这些代码从 JS 重写为 TS? is there any simple property or class for this HashTable in typescript?在打字稿中,此HashTable是否有任何简单的属性或类?

Or any other properties in TS to do the same HashTable functionality?或者 TS 中的任何其他属性来执行相同的HashTable功能?

Modern JavaScript has three options:现代 JavaScript 有三个选项:

  • Map , is closest to HashTable to my knowlage. Map ,据我所知最接近 HashTable 。 It main advantage is that it's keys may be of type Object .它的主要优点是它的键可能是Object类型。
  • Set , it is basically an unique array. Set ,它基本上是一个唯一的数组。
  • Object also known an {} . Object也称为{} Key value store.键值存储。

I would suggest to use object, but if your keys need to be object, use the Map .我建议使用 object,但如果您的键需要是 object,请使用Map


JavaScript Object , or {} , is about 20x faster than Map . JavaScript Object{}Map快约 20 倍。 So use Map only if you need to use objects as keys.因此,仅当您需要使用对象作为键时才使用Map

Map is probably the right answer as suggested above, but maybe a hashmap with types looks something like this would work: Map 可能是上面建议的正确答案,但也许带有类型的 hashmap 看起来像这样可以工作:

{ [key: string]: Type; }

or

{ [key: number]: Type; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM