-1

This is the main problem:

var obj = {
  "22": false,
  "32": true,
}

console.log(obj.32) //<---- NOT WORKING??!?!?!

Why can't I reach the part of the object??

I know you can't begin variables with numbers but you can with object parts, so how can I read this?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
user2336726
  • 163
  • 1
  • 1
  • 4
  • 1
    Don't use a `,` at the end of an object definition. Old versions of IE break. It is allowed in ECMAScript5, but it's easier to just not include it – Ian May 09 '13 at 15:41

3 Answers3

5

Just use:

console.log(obj["32"]);

DEMO: http://jsfiddle.net/WrzbV/1/

Or obj[32] - the 32 will be converted to a string and will be found just the same as using "32".

There are 2 ways to access an object by property name - bracket notation (what I suggested) and dot notation (what you're using). With dot notation, you must use a valid identifier, which 32 is not...just like you can't do var 32 = "whatever";

Reference:

Community
  • 1
  • 1
Ian
  • 50,146
  • 13
  • 101
  • 111
-1

You will not be able to access properties with numbers

either change the key to string

 var obj = {
    "i": false,
    "j": true,
}

 console.log(obj.i);
 console.log(obj.j);

or use

console.log(obj["32"])

Or if you can use jQuery then use

  $.each(obj, function(key,value){

    console.log(key +' --  '+ value);

  });
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
-1

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($). in JavaScript, identifiers can't begin with a numeral.

As already said, use array syntax to access the object property as obj["32"]

Varinder Singh
  • 1,570
  • 1
  • 11
  • 25