How to access object property beginning with a number (SyntaxError: Unexpected identifier)

后端 未结 1 1343
不知归路
不知归路 2021-01-26 13:59

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(dat         


        
相关标签:
1条回答
  • 2021-01-26 14:14
    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.

    0 讨论(0)
提交回复
热议问题