typescript 是否有打字稿 List<> 和/或 Map<> 类/库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23096260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:13:38  来源:igfitidea点击:

Is there a typescript List<> and/or Map<> class/library?

collectionstypescript

提问by David Thielen

Did they add a runtime List<> and/or Map<> type class to typepad 1.0? And if not, is there a solid library out there someone wrote that provides this functionality?

他们是否向 typepad 1.0 添加了运行时 List<> 和/或 Map<> 类型类?如果没有,是否有人编写了一个可靠的库来提供此功能?

And in the case of List<>, is there a linked list where the elements in the list have the next/prev property? We need a list where from an element object (ie not from an iterator), we can get the next and previous elements in the list (or null if it's the first/last one).

在 List<> 的情况下,是否有一个链表,其中列表中的元素具有 next/prev 属性?我们需要一个列表,其中来自元素对象(即不是来自迭代器),我们可以获取列表中的下一个和前一个元素(如果是第一个/最后一个,则为 null)。

采纳答案by basarat

Did they add a runtime List<> and/or Map<> type class to typepad 1.0

他们是否向 typepad 1.0 添加了运行时 List<> 和/或 Map<> 类型类

No, providing a runtime is not the focus of the TypeScript team.

不,提供运行时不是 TypeScript 团队的重点。

is there a solid library out there someone wrote that provides this functionality?

有人写了一个可靠的库来提供这个功能吗?

I wrote (really just ported over buckets to typescript): https://github.com/basarat/typescript-collections

我写了(实际上只是将桶移植到打字稿):https: //github.com/basarat/typescript-collections

Update

更新

JavaScript / TypeScript now support this natively and you can enable them with lib.d.ts: https://basarat.gitbooks.io/typescript/docs/types/lib.d.ts.htmlalong with a polyfill if you want

JavaScript / TypeScript 现在原生支持它,你可以通过以下方式启用它们lib.d.tshttps: //basarat.gitbooks.io/typescript/docs/types/lib.d.ts.html以及一个 polyfill 如果你愿意

回答by Nitzan Tomer

It's very easy to write that yourself, and that way you have more control over things.. As the other answers say, TypeScript is not aimed at adding runtime types or functionality.

自己编写它非常容易,这样您就可以更好地控制事物。正如其他答案所说,TypeScript 的目的不是添加运行时类型或功能。

Map:

地图:

class Map<T> {
    private items: { [key: string]: T };

    constructor() {
        this.items = {};
    }

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

    has(key: string): boolean {
        return key in this.items;
    }

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

List:

列表:

class List<T> {
    private items: Array<T>;

    constructor() {
        this.items = [];
    }

    size(): number {
        return this.items.length;
    }

    add(value: T): void {
        this.items.push(value);
    }

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

I haven't tested (or even tried to compile) this code, but it should give you a starting point.. you can of course then change what ever you want and add the functionality that YOU need...

我还没有测试(甚至试图编译)这段代码,但它应该给你一个起点..你当然可以改变你想要的任何东西并添加你需要的功能......

As for your "special needs" from the List, I see no reason why to implement a linked list, since the javascript array lets you add and remove items.
Here's a modified version of the List to handle the get prev/next from the element itself:

至于列表中的“特殊需求”,我认为没有理由实现链表,因为 javascript 数组允许您添加和删除项目。
这是 List 的修改版本,用于处理元素本身的 get prev/next:

class ListItem<T> {
    private list: List<T>;
    private index: number;

    public value: T;

    constructor(list: List<T>, value: T, index: number) {
        this.list = list;
        this.index = index;
        this.value = value;
    }

    prev(): ListItem<T> {
        return this.list.get(this.index - 1);
    }

    next(): ListItem<T> {
        return this.list.get(this.index + 1);   
    }
}

class List<T> {
    private items: Array<ListItem<T>>;

    constructor() {
        this.items = [];
    }

    size(): number {
        return this.items.length;
    }

    add(value: T): void {
        this.items.push(new ListItem<T>(this, value, this.size()));
    }

    get(index: number): ListItem<T> {
        return this.items[index];
    }
}

Here too you're looking at untested code..

在这里,您也在查看未经测试的代码。

Hope this helps.

希望这可以帮助。



Edit - as this answer still gets some attention

编辑 - 因为这个答案仍然得到一些关注

Javascript has a native Map objectso there's no need to create your own:

Javascript 有一个本机Map 对象,因此无需创建自己的对象:

let map = new Map();
map.set("key1", "value1");
console.log(map.get("key1")); // value1