Hashset in TypeScript

I am trying to do something very basic in TypeScript. Get a list of unique strings when parsing the map, as indicated in this post.

Here is what I am trying 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 listing will always evaluate to false for any row, duplicate or not. Here are some examples:

 ## 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 ends, my list contains duplicates. I tried looking at this documentation , but not hashset or any set at all.

Is there something equivalent in TypeScript typing? ie List of unique items

+4
source share
3 answers

He exists!

mySet: Set<string> = new Set<string>();
+9
source

{} - , - JavaScript, TypeScript , JS .

, :

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

I found my 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 if this is a better solution than any so far, but this is what I decided to stick with until a better one is selected.

+1
source

Source: https://habr.com/ru/post/1658469/


All Articles