mro
mro

Reputation: 141

String from jquery array element

I'm looking at arrays in jquery and have this issue, I need to assign a key with a town name, but struggling to understand how to deal with the spaces.

var hashtable = {};
hashtable['Bognor Regis'] = ["lat=50.782998&lng=-0.673061","Sussex"];
var str = hashtable.Bognor Regis[0];

alert(str);

I thought perhaps I could do this

hashtable['Bognor-Regis'] = ["lat=50.782998&lng=-0.673061","Sussex"];

var str = hashtable.Bognor-Regis[0];

then remove the - later, but it only seems to work if i have something like this

hashtable['BognorRegis'] = ["lat=50.782998&lng=-0.673061","Sussex"];

What's the correct way of doing this ?

Thanks

Upvotes: 1

Views: 107

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

If the keys have spaces you have to use the array accessor to retrieve them:

var hashtable = {};
hashtable['Bognor Regis'] = ["lat=50.782998&lng=-0.673061","Sussex"];
var str = hashtable['Bognor Regis'][0];

alert(str);

Example fiddle

Upvotes: 2

Related Questions