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?


最低支持级别:IE 9+、jQuery(当前)

推荐答案

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

恒定时间查找功能可以实现为:

var hash = {
  1:true,
  2:true,
  7:true
  //etc...
};

var checkValue = function(value){
  return hash[value] === true;
};


checkValue(7); // => true
checkValue(3); // => false

Jquery相关问答推荐

多个 AJAX 调用;获取所有失败的呼叫

如何使用 jQuery 强制悬停状态?

Flask,如何为ajax调用返回成功状态码

jQuery找到最近的匹配元素

jQuery 1.4.1 中缺少 JSON 字符串化?

document.querySelector 一个元素中的多个数据属性

jQuery 动画滚动

将输入更改为大写

jQuery:获取父母,父母ID?

jQuery UI 工具提示不支持 html 内容

你如何在Javascript中缓存图像

jQuery表格行中的每个循环

如何隐藏 Twitter Bootstrap 下拉菜单

如何使用 jQuery 或纯 JS 重置所有复选框?

通过 :not 在 jQuery Select 器中隐藏除 $(this) 之外的所有内容

如何使用 JQuery $.scrollTo() 函数滚动窗口

使用 jquery 禁用文本框?

jquery $(window).height() 正在返回文档高度

catch forEach 最后一次迭代

jQuery / Javascript - 如何将像素值 (20px) 转换为数值 (20)