如何在函数中使用JavaScript哈希作为参数?

如何在函数中使用JavaScript哈希作为参数?,javascript,hash,Javascript,Hash,我是新来的 我正在使用一个函数,该函数的文档说“函数的参数必须是JavaScript哈希,其中键是要过滤的(数据库)字段,值是字符串或字符串数组” 适用于我的示例: //New York Knicks fan.... viz.filter({CitiesILoathe: ['Boston']}); viz.filter({CitiesILoathe: ['Boston','Miami']}); 这两个代码片段中的任何一个都适用于我,在filter()完成后,从我看到的内容中删除上述城市 现在我

我是新来的

我正在使用一个函数,该函数的文档说“函数的参数必须是JavaScript哈希,其中键是要过滤的(数据库)字段,值是字符串或字符串数组”

适用于我的示例:

//New York Knicks fan....
viz.filter({CitiesILoathe: ['Boston']});
viz.filter({CitiesILoathe: ['Boston','Miami']});
这两个代码片段中的任何一个都适用于我,在filter()完成后,从我看到的内容中删除上述城市

现在我想直接传入一个我先前创建/填充的哈希

不知道怎么做

我试过:

var CitiesILoathe= new Object(); //my "hash"
CitiesILoathe['Boston'] = 1;
CitiesILoathe['Miami'] = 2;
viz.filter({CitiesILoathe}); // also tried same thing w/o curly braces
…但没有快乐。我一直在搜索文档,但我的JavaScript词汇/智能在这一点上很慢,我真的不知道我在寻找什么

有人能帮我往正确的方向推吗?非常感谢

您需要一个对象(名称无关紧要,因此我将其称为x),该对象有一个名为
CitiesILoathe
的属性。该属性是一个数组,因此可以使用来添加:

var x= { "CitiesILoathe" : [
    "Boston",
    "Miami"
] };
viz.filter(x);
var x = {CitiesILoathe: []};
x.CitiesILoathe.push('Boston');
x.CitiesILoathe.push('Miami');
viz.filter(x);
您也可以这样做:

var CitiesILoathe = [];
CitiesILoathe.push('Boston');
CitiesILoathe.push('Miami');
viz.filter({CitiesILoathe: CitiesILoathe});
您需要一个对象(名称无关紧要,所以我将其称为x),它有一个名为
CitiesILoathe
的属性。该属性是一个数组,因此可以使用来添加:

var x = {CitiesILoathe: []};
x.CitiesILoathe.push('Boston');
x.CitiesILoathe.push('Miami');
viz.filter(x);
您也可以这样做:

var CitiesILoathe = [];
CitiesILoathe.push('Boston');
CitiesILoathe.push('Miami');
viz.filter({CitiesILoathe: CitiesILoathe});
“散列”是对象本身。。“键”是字段,在本例中是“CitieSloath”,这些键的值是一个数组,用字符串填充


“散列”是对象本身。。“键”是字段,在本例中是“CitieSloath”,这些键的值是一个数组,用字符串填充。

感谢您的补充说明。谢谢你的解释。谢谢。