Getting a Custom Objects properties by string var

JavascriptPropertiesCustom Object

Javascript Problem Overview


> Possible Duplicate:
> javascript object, access variable property name?

Trying to get more advanced in my JS...

I have a custom object:

Object myObject = new Object();

myObject.thing = anythingHere;

I would like to be able to retrieve a custom objects property by passing in a string... eg:

var propertyString = 'thing';
alert(myObject.propertyString);

I can't quite figure that out. I've looked at a number of tutorials for custom objects - but nothing shows how to get properties that I don't know the names of... Also - I would like to avoid looping through all properties if possible...

Thanks!!!

Javascript Solutions


Solution 1 - Javascript

Simply use myObject['thing'].

Solution 2 - Javascript

You could use:

myObject[propertyString] ;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJay-Nicolas HacklemanView Question on Stackoverflow
Solution 1 - JavascriptldiqualView Answer on Stackoverflow
Solution 2 - JavascriptRyanView Answer on Stackoverflow