J86
J86

Reputation: 15237

Can't get value from Dictionary like object in JS

I have the following object:

a list of objects in JS

Having the name of the county, I want to quickly get the population.So with a bit of code I got from here, I want to be able to quickly get the population of each county (the number bit). So I try to do this:

var lookup = {};
var len    = cdata.length;
var i;
for (i = 0; i < len; i++) {
    lookup[cdata[i].county.replace(/ /g,'').toLowerCase()] = cdata[i].count;
}

My lookup object is now this:

Object {devon: "747900", suffolk: "730100", westyorkshire: "2227400", kent: "1466500", lancashire: "171600"}

Then when I try to obtain the population number like so:

lookup[devon]

I get the following error:

Uncaught TypeError: object is not a function

Why isn't the above working? Also is that the best way to quickly get at the population I need?

Upvotes: 3

Views: 521

Answers (1)

Hoijof
Hoijof

Reputation: 1413

You have to address to object properties like this:

lookup["devon"] or lookup.devon

Upvotes: 3

Related Questions