8

Hi I'm trying to access an js object property which has an slash "/" in its name.

The object its somthing like:

{
   my/key : "my value"
   // more stuff here...
}

I try the following construction:

myObject["my/key"]

If I try to it in Chrome DevTools it works correctly but when I execute my code i get a beautiful undefined on browser console (using console.log())

has anybody any idea of what's happening? :S

SergiGP
  • 669
  • 7
  • 17
  • Try using backslash before slash, this could help. – Jan.J Aug 22 '13 at 12:09
  • 1
    / is not an illegal character - see http://stackoverflow.com/questions/1661197/valid-characters-for-javascript-variable-names for a comprehensive description of what is valid. – knolleary Aug 22 '13 at 12:09
  • 2
    Pretty much anything can go into property name (including newlines for example `a = {}; a["\n"] = 'foo';` works) as long as you enclose it in quotes and use array access operator. JS objects are key-value maps essentially, and keys can be anything. – Mchl Aug 22 '13 at 12:20

2 Answers2

8

When you enclose the prop name into quotes, it works also in the code:

var obj = {
    'my/key' : 'my value'
};

You can check this at jsFiddle.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • 2
    This is what you want to do; without quotes, it's a division operator. –  Aug 22 '13 at 12:22
0

I tried your code in Node.js and it is working as expected, as long as the property name is quoted.

Try outputting the exact value of the key ("my/key") that you use to access the value - maybe, you are using a different key there.

Baradzed
  • 578
  • 3
  • 10