如何检查hashset
中的javascript
是否包含特定值?我尝试了以下不起作用的方法:
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的原型,例如:
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