What is the JavaScript equivalent to a C# HashSet?

后端 未结 6 1436
天涯浪人
天涯浪人 2020-12-13 23:39

I have a list of a few thousand integer keys. The only thing I need to do with this list is say whether or not a given value is in the list.

For C# I would use a

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 00:08

    You can use just a regular JavaScript object and the 'in' keyword to see if that object has a certain key.

    var myObj = {
      name: true,
      age: true
    }
    
    'name' in myObj //returns true;
    'height' in myObj // returns false;
    

    Or if you know you're going to have keys in your object that might be built in JavaScript object properties use...

    var myObj = {
      name: true,
      age: true
    }
    
    myObj.hasOwnProperty('name') //returns true;
    myObj.hasOwnProperty('height') // returns false;
    

提交回复
热议问题