What is the JavaScript equivalent to a C# HashSet?

后端 未结 6 1443
天涯浪人
天涯浪人 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:07

    Under the hood, the JavaScript Object is implemented with a hash table. So, your Key:Value pair would be (your integer):true

    A constant-time lookup function could be implemented as:

    var hash = {
      1:true,
      2:true,
      7:true
      //etc...
    };
    
    var checkValue = function(value){
      return hash[value] === true;
    };
    
    
    checkValue(7); // => true
    checkValue(3); // => false
    

提交回复
热议问题