[Fixed] Hashset in TypeScript

Issue

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.

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:

 ## 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

Solution

It exists!

mySet: Set<string> = new Set<string>();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Leave a Reply

(*) Required, Your email will not be published