Shamoon
Shamoon

Reputation: 43569

How can I reference objects with keys that have dots in JavaScript?

var ageRanges;
ageRanges = {
  '18.20': 0,
  '21.24': 0,
  '25.34': 0,
  '35.44': 0,
  '45.54': 0,
  '55.64': 0,
  '65+': 0
};

I want to access ageRanges.'18.20', but that gives me an error: TypeError: Cannot read property '0' of null - so what's the proper way to access it?

Upvotes: 0

Views: 147

Answers (4)

Kit Ho
Kit Ho

Reputation: 26998

use ageRanges["18.20"]; to access

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160261

ageRanges["18.20"];

Easy-peasy.

Upvotes: 3

robyaw
robyaw

Reputation: 2321

Use ageRanges['18.20'] to access the property. This is one of two ways to access JavaScript object properties. The alternative and recommended way is dot-notation, which fails in this instance due to the period character in your property names.

Upvotes: 3

Luke
Luke

Reputation: 8407

you have to use it this way:

ageRanges['18.20']

Upvotes: 4

Related Questions