Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 103305  / 10 Years ago, fri, june 13, 2014, 12:00:00

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 HashSet to make that look-up fast. What's the JavaScript equivalent?






Minimal support level: IE 9+, jQuery (current)


More From » jquery

 Answers
12

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

[#70592] Wednesday, June 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;