Dictionaries

W

WilsonOfCanada

Hellos,

I have a dictionary (at least in python sense) that I am trying to use
exist(). I was wondering if this what a dictionary looks like because
the exist does not seem to work with it.

var list_provinces = {'Canada': ['British Columbia', 'Alberta',
'Manitoba', 'New Brunswick', 'Newfoundland and Labrador', 'Nova
Scotia', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan',
'Northwest Territories', 'Nunavut', 'Yukon']};

var found = list_provinces.Exists(selectedCountry);

Thanks
 
R

RobG

Hellos,

I have a dictionary (at least in python sense) that I am trying to use
exist().  I was wondering if this what a dictionary looks like because
the exist does not seem to work with it.

var list_provinces = {'Canada': ['British Columbia', 'Alberta',
'Manitoba', 'New Brunswick', 'Newfoundland and Labrador', 'Nova
Scotia', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan',
'Northwest Territories', 'Nunavut', 'Yukon']};

var found = list_provinces.Exists(selectedCountry);

You are looking for the - in - operator, e.g.:

alert('Canada' in list_provinces); // true

Checking for a province takes a little more logic, but you could
implement countries as objects and make provinces properties, so you
could use - in - there too:

if ( selectedCountry in list_provinces &&
selectedProvince in list_provinces[selectedCountry] )
{
// do stuff
}

You may want to get a bit more sophisticated and create a
provincesList object that has methods to get and set countries and
their provinces (or states or whatever) and test for their existance.
Depends on the functionality you require and how often it's used.
 
T

Thomas 'PointedEars' Lahn

RobG said:
I have a dictionary (at least in python sense) that I am trying to use
exist(). I was wondering if this what a dictionary looks like because
the exist does not seem to work with it.

var list_provinces = {'Canada': ['British Columbia', 'Alberta',
'Manitoba', 'New Brunswick', 'Newfoundland and Labrador', 'Nova
Scotia', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan',
'Northwest Territories', 'Nunavut', 'Yukon']};

var found = list_provinces.Exists(selectedCountry);

You are looking for the - in - operator, e.g.:

alert('Canada' in list_provinces); // true

However,

/* true */
window.alert('toString' in list_provinces);

because the `in' operator (JavaScriptâ„¢ 1.4+, JScript 5.0+, ECMAScript 3+)
considers the prototype chain. If all property values are true-values (like
here),

list_provinces["Canada"]

is semantically equivalent to the above. The proper, but not as compatible,
way to determine if an object has a (non-inherited) property (regardless of
value) is

list_provinces.hasOwnProperty("Canada")

(available in JavaScriptâ„¢ 1.5+, JScript 5.5.6330+, ECMAScript 3+,
JavaScriptCore 525.13+, Opera 5.02+, KJS 3.5.9+). A more compatible
alternative in JavaScriptâ„¢ is to determine if the object's prototype does
not have or inherit a property that the object has:

('Canada' in list_provinces) && !('Canada' in list_provinces.__proto__)

or

list_provinces['Canada'] && !list_provinces.__proto__['Canada']

if the property value is supposed to be a true-value.

Other implementations, including JavaScriptâ„¢ 1.8.1+, may provide the
Object.getPrototypeOf() method, as defined in the ECMAScript 5 Final Draft,
to return a reference to the object's prototype (instead). However, in that
case, Object.prototype.hasOwnProperty() is the safer approach, of course.

<http://PointedEars.de/es-matrix>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top