Accessing Object's Data Via Another Variable Name

It's super late and my mind is blanking right now, but let's say I have variable filename and it's storing the name of another variable marker. The variable marker is an array and contains the object & property position: new google.maps.LatLng(42.2550,-114.3221).

I've been stupidly trying to access it via filename.position which of course returns undefined, since it's searching the literal filename for a 'position' property that does not exist.

But how could I pull marker.position by using filename? Is there some nifty jQuery trick for, uh, 'resolving' a variable to its contents? I'm brain fried. I know I've done this before.


If it's possible in your script, you can store the data not just in variable, but in a property of some object (usually it's more convenient to use global one). For example

var myObj = {};
myObj.marker = new google.maps.LatLng(42.2550,-114.3221); // or anything else

Then you will be able to get this property using a variable like this:


In this case i would also recomment to check for myObj[filename] existance using typeof structure, just to make sure such property exists in myObj.

if (typeof myObj[filename] !== "undefined") {
  // do something

As apsillers noted, you could use global window object for this as well. But if your marker variable was defined inside some other function (i.e. not global), you won't be able to access it with window.marker or window[filename] as it will be out of scope.

Second way is to use eval() function which i'd strongly recommend to avoid.

Try this :


Need Your Help

jQuery mobile adds additional scrollbar to page

jquery html5 mobile scrollbar

I’m currently working with jQuery mobile (for the first time).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.