Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing an object property that contains forward slashes

Tags:

javascript

I have created JSON by using the json_encode PHP function. The key of one of the items of the array contains a forward slash and when the JSON is parsed, the object looks like this when output in Chrome's console.

Object
contact/allow_anonymous: "0"
menulayout: "horizontal"
pages/max_pages: "10"
primarycolour: "329e95"
websitelogo: "text"

My problem is that I can't seem to be able to access the value of the properties that have a forward slash in them.

Any ideas? Since javascript allowed me to create the object I would assume there is a way to retrieve the values.

like image 302
Gabriel Spiteri Avatar asked Apr 25 '12 17:04

Gabriel Spiteri


People also ask

How do you turn on forward slash in regex?

Answers. You need to escape the forward slash in regular expression by adding a backslash infornt of it.

What operator do you use to access the methods and properties of an object?

Object properties and methods can be accessed using dot notation or [ ] bracket.


1 Answers

Just use myObject["key"] instead of myObject.key:

alert(myObject["contact/allow_anonymous"]);
like image 102
ComFreek Avatar answered Sep 19 '22 17:09

ComFreek