Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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_Hash - Fatal编程技术网

javascript中的哈希是什么?如何对数组使用哈希?

javascript中的哈希是什么?如何对数组使用哈希?,javascript,hash,Javascript,Hash,我在我的对象原型中使用了一个数组,它基本上附带了添加、删除和搜索功能 像这样的 myobj = function() { this.Array_ = []; } myobj.prototype.add = function(item) { goog.array.insert(this.Array_, item); } myobj.prototype.hasItem = function(item) { goog.array.contains(this.Array_, item);

我在我的对象原型中使用了一个数组,它基本上附带了添加、删除和搜索功能

像这样的

myobj = function() {
  this.Array_ = [];
}

myobj.prototype.add = function(item) {
  goog.array.insert(this.Array_, item);
}

myobj.prototype.hasItem = function(item) {
  goog.array.contains(this.Array_, item);
}
在我的例子中,一个样本数组是整数列表。[1,2,3,4]

但后来我了解到,这是非常昂贵的,可以节省成本,如果我使用哈希。 有人能用上面的例子解释一下hash的用法吗?

单词“hash”有很多含义,但在本例中,它可能指的是一般的javascript对象,它们在内部是“hashtable”。对象具有内置的“添加”和“包含”功能:

foo = {}

foo['x'] = 1   // "add"
'x' in foo     // "contains"
但是,请注意,键始终转换为字符串,因此,如果需要其他类型的键(例如,通用对象),则必须使用自定义函数,例如:

contains = function(ary, obj) {
    return ary.indexOf(obj) >= 0;
}

add = function(ary, obj) {
    if (!contains(ary, obj))
        ary.push(obj)
}

听起来像是你在使用它。@Sarfraz解决了我的问题thanksBy“无法将对象存储在哈希表中”你是说“无法将对象存储为哈希表中的键”?