1

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

1 Answer 1

2

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

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

Comments

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.