Reputation: 1707
JS Newb here.
I'm working with a function, and the documentation for said function says "The argument to the function must be a JavaScript hash where the key is the (database) field to be filtered, and the value is either a string or an array of strings".
Examples that work for me:
//New York Knicks fan....
viz.filter({CitiesILoathe: ['Boston']});
viz.filter({CitiesILoathe: ['Boston','Miami']});
Either of these snippets work for me, removing said cities from what I see after filter() completes.
Now I want to DIRECTLY pass in a hash which I create/populate earlier.
Can't figure out how.
I've tried:
var CitiesILoathe= new Object(); //my "hash"
CitiesILoathe['Boston'] = 1;
CitiesILoathe['Miami'] = 2;
viz.filter({CitiesILoathe}); // also tried same thing w/o curly braces
...but no joy. I've been searching thru docs, but my JavaScript vocabulary/intelligence is slow low at this point, I really don't know what I'm looking for.
Can anyone give me a push in the right direction? Many Thanks!
Upvotes: 2
Views: 3805
Reputation: 19344
//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)
The "Hash" is the object itself.. the "keys" are the fields, in this case "CitiesILoath" and the value for those keys is an array, filled with strings.
Upvotes: 2
Reputation: 141927
You want an object (it's name doesn't matter, so I'll call it x) which has one property called CitiesILoathe
. That property is an array, so you can use push to add to it:
var x = {CitiesILoathe: []};
x.CitiesILoathe.push('Boston');
x.CitiesILoathe.push('Miami');
viz.filter(x);
You can also do this like:
var CitiesILoathe = [];
CitiesILoathe.push('Boston');
CitiesILoathe.push('Miami');
viz.filter({CitiesILoathe: CitiesILoathe});
Upvotes: 1
Reputation: 6947
var x= { "CitiesILoathe" : [
"Boston",
"Miami"
] };
viz.filter(x);
Upvotes: 1