TypeScript 中的哈希集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40180641/
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-10-21 03:56:44  来源:igfitidea点击:

Hashset in TypeScript

angulartypescript

提问by angryip

I am attempting to do something very basic in TypeScript. Get a list of unique strings while parsing a map as referenced in this post.

我正在尝试在 TypeScript 中做一些非常基本的事情。在解析本文中引用的地图时获取唯一字符串列表。

Here is what I am attempting to do:

这是我正在尝试做的事情:

let myData = new Array<string>();
for (let myObj of this.getAllData()) {
    let name = myObj.name;
    console.log("## we have " + name);
    console.log("### is property ? " + myData.hasOwnProperty(name));
    if (!myData.hasOwnProperty(name)){
        myData.push(name);
    }
}

My printout will always evaluate to false for any string, duplicate or not. Here is some sample output:

对于任何字符串,无论是否重复,我的打印输出将始终评估为 false。这是一些示例输出:

 ## we have COW
 ### is property ? false
 ## we have COW
 ### is property ? false
 ## we have RAODN
 ### is property ? false
 ## we have COOL
 ### is property ? false

However, when the process completes, my list contain duplicates. I have tried looking at this documentation, but there is no mention of a 'hashset' or any set in general.

但是,当该过程完成时,我的列表包含重复项。我曾尝试查看此文档,但总体上没有提及“哈希集”或任何集。

Is there something equivalent in TypeScript of a Set? i.e A list of unique elements

Set 的 TypeScript 中是否有等价的东西?即唯一元素列表

回答by Jesse Palmer

The title of this post asks for a HashSet. The Set JavaScript implementation loops over each element in the Set, which would be O(n). For a data structure to be a HashSet, it should be close to O(1). If you are interested in a HashSet, it's pretty easy to create it in TypeScript. Here is a basic implementation:

这篇文章的标题要求一个 HashSet。Set JavaScript 实现循环遍历 Set 中的每个元素,这将是 O(n)。对于要成为 HashSet 的数据结构,它应该接近 O(1)。如果您对 HashSet 感兴趣,在 TypeScript 中创建它非常容易。这是一个基本的实现:

class HashSet {
    private values = {}

    add(val) {
        this.values[val] = true
    }

    has(val) {
        return this.values[val] === true
    }

    remove(val) {
       delete this.values[val]
    }

    getValues() {
        return Object.keys(this.values)
    }
}

const hashSet = new HashSet()
hashSet.add(5)
hashSet.add(10)
console.log(hashSet.has(5)) // true
console.log(hashSet.getValues()) // [5, 10]
hashSet.remove(5)
console.log(hashSet.has(5)) // false

回答by silentsod

An object {} is a key value pair dictionary is a hash set in JavaScript which TypeScript is a superset of and therefore you can use a JS object to serve in this role.

对象 {} 是键值对字典是 JavaScript 中的哈希集,TypeScript 是其超集,因此您可以使用 JS 对象来充当此角色。

A quick version from the code you posted:

您发布的代码的快速版本:

let myData = {};
for (let myObj of this.getAllData()) {
    let name = myObj.name;
    if (!myData[name]){
        myData[name] = name;
    }
}

回答by angryip

I found my own solution with something like this:

我找到了自己的解决方案,如下所示:

let myData = new Array<string>();
for (let myObj of this.getAllData()) {
    let name = myObj.name;
    if (myData.indexOf(name) == -1){
        myData.push(name);
    }
}

Not sure that it is a better solution than any so far, but it is something I have decided to stick with until a better one is chosen.

不确定它是否比迄今为止的任何解决方案都更好,但我决定坚持使用它,直到选择更好的解决方案为止。