Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查元素是否在哈希集中_Javascript_Jquery_Hashset - Fatal编程技术网

Javascript 检查元素是否在哈希集中

Javascript 检查元素是否在哈希集中,javascript,jquery,hashset,Javascript,Jquery,Hashset,如何在javascript中检查hashset是否包含特定值?我尝试了以下不起作用的方法: if (hashset.contains(finalDate)) { alert("inside if"); } 我的js代码: $.each(cdata.lines, function(idx, line){ // line.hashsetvariable is my hashset which contain all dates and // let finaldate i

如何在
javascript
中检查
hashset
是否包含特定值?我尝试了以下不起作用的方法:

if (hashset.contains(finalDate)) {
    alert("inside if");
}
我的js代码:

$.each(cdata.lines, function(idx, line){
    // line.hashsetvariable is my hashset which contain all dates and 
    // let finaldate is 2012-19-12 
    // I want to check this date in my hashset.
}

如果您所指的哈希集是一个对象(或哈希…),则可以通过以下方式检查它是否包含密钥:

var hash = { foo: 'bar', baz: 'foobar' };
'foo' in hash;
如果您寻找特定的价值:

function containsValue(hash, value) {
    for (var prop in hash) {
        if (hash[prop] === value) {
            return true;
        }
        return false;
    }
}
如果您想做一些更“全局”的事情(我不推荐!),您可以更改对象的原型,如:

Object.prototype.containsValue = function (value) {
    for (var prop in this) {
        if (this[prop] === value) {
            return true;
        }
    }
    return false;
}
在这种情况下:

var test = { foo: 'bar' };
test.containsValue('bar'); //true
test.containsValue('foobar'); //false

发布定义您的hashset和finalDate的代码您可以发布
hashset
finalDate
的值吗
hashset
是否来自某种jQuery插件?是的,我的js代码如下:$。每个(cdata.lines,function(idx,line){这里line.hashsetvariable是我的hashset.}这并不能真正解释什么是
hashset
。JavaScript中没有“hashset”,因此必须明确使用哪些数据结构/类型。