什么是JavaScript等同于C#HashSet? [英] What is the JavaScript equivalent to a C# HashSet?

查看:101
本文介绍了什么是JavaScript等同于C#HashSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个几千个整数键的列表.我唯一需要处理的就是说列表中是否有给定值.

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.

对于C#,我将使用HashSet来快速进行查找.什么是JavaScript等效项?

For C# I would use a HashSet to make that look-up fast. What's the JavaScript equivalent?

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

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

推荐答案

在后台,JavaScript对象是通过哈希表实现的. 因此,您的Key:Value对将是(your integer):true

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

这篇关于什么是JavaScript等同于C#HashSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆