0

I have an object within another object, which im trying to get the value but it always returns "unexpected identifier".

snow: Object {3h: 1.3}

console.log(data.snow.3h) //returns Uncaught SyntaxError: Unexpected identifier

console.log(data.snow) //returns Object {3h: 1.3}

So how can i get the value of 3h ?

m59
  • 43,214
  • 14
  • 119
  • 136
Lulli240
  • 3
  • 1

1 Answers1

3
data.snow['3h'];

Properties accessed with dot notation can't begin with a number.

snow: Object {3h: 1.3} could be refactored to snow: {3h: 1.3}. It is redundant to type Object.

Also, if you wrap your property names in quotes, you can use bizarre property names like:

var myObj = {
  '^': 'foo'
};
console.log(myObj['^']);

but, I generally stick to more standard names that I can access with dot notation.

m59
  • 43,214
  • 14
  • 119
  • 136
  • Well thats awkward, really thought I tried ['3h']. Its information from API, I usually stick to standard names. But thanks alot :) – Lulli240 Dec 02 '13 at 19:41