2

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!

3 Answers 3

2
//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.

Sign up to request clarification or add additional context in comments.

Comments

1
var x= { "CitiesILoathe" : [
    "Boston",
    "Miami"
] };
viz.filter(x);

Comments

1

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});

1 Comment

Thanks for the additional explanation. Appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.