gpt4 book ai didi

javascript - 如何使用 JavaScript 散列作为函数中的参数?

转载 作者:行者123 更新时间:2023-11-29 18:29:01 25 4
gpt4 key购买 nike

这里是 JS 新手。

我正在使用一个函数,该函数的文档说“函数的参数必须是 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 词汇量/智能很低,我真的不知道我在找什么。

任何人都可以在正确的方向上插入我吗?非常感谢!

最佳答案

//inline declaration
var CitiesILoath = {
"CitiesILoath": [
"Boston"
,"Miami"
]
}
viz.filter(CitiesILoath)

//ad-hoc
var myFilter = {}; //same as "new Object()"
myFilter["CitiesILoath"] = []; //same as "new Array()"
myFilter["CitiesILoath"].push("Boston"); //append to array
myFilter["CitiesILoath"].push("Miami"); //append to array

//or, use decimal notation
var myFilter = {}; //same as "new Object()"
myFilter.CitiesILoath = []; //same as "new Array()"
myFilter.CitiesILoath.push("Bostom"); //append to array
myFilter.CitiesILoath.push("Miami"); //append to array
viz.filter(myFilter)

“哈希”是对象本身。“键”是字段,在本例中是“CitiesILoath”,这些键的值是一个数组,由字符串填充。

关于javascript - 如何使用 JavaScript 散列作为函数中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10130319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com