1

I have two variables, one holds extra info about an object, and is named extra_info + the id of the object. The other holds the name to that variable:

selector = 'extra_info' + subscid;

I am trying to access the data stored in the variable that holds the extra info, but I can't seem to access it. Stupid, I know :-)

EDIT: So, the name of the variable that I in the end need to access is:

extra_infoXXXXX

where XXXXX is stored in subscid.

Borniet
  • 3,544
  • 4
  • 24
  • 33

3 Answers3

2

No quotes:

selector = extra_info + subscid;

Or, and I'm loathe to suggest this because it's a red flag of bad design, you can use eval():

selector = eval('extra_info' + subscid);

(Obilgatory "eval is evil" link)


EDIT

It sounds like you should stored your extra_info in an array object, with the subscid for its indexes properties!

To access, do something like extra_info[subscid].

Ben
  • 54,723
  • 49
  • 178
  • 224
  • 3
    Downvotes without explanation = the best. – Ben May 03 '13 at 06:08
  • Indeed, hate it when they do that! As for the variable thing :-) I'm storing it all in an array now. What it is actually doing, I have a page with a form. This does an ajax request, and shows the results in a compact form. I want the full details (stored in extra_info[]) to show in an overlay, that is loaded upon clicking. So my AJAX returnscript adds a to each result. – Borniet May 03 '13 at 06:25
  • @FabrícioMatté, that's not accurate. It's undefined because the function doesn't return anything. Try `(function(){ var foo=42; return eval('foo'); }());` – Dagg Nabbit May 03 '13 at 06:27
  • @DaggNabbit Oh I stand corrected. I thought I had put a `console.log` in there, my bad. Removed previous comment's misleading information. – Fabrício Matté May 03 '13 at 06:28
  • @Steve: *"It sounds like you should stored your extra_info in an array, with the subscid for indexes!"* Not an array and indexes. An object and properties. – T.J. Crowder May 03 '13 at 06:32
  • 1
    @T.J.Crowder - terminology = important. Edited. Also not sure why the exclamation point. I guess the light bulb moment just overwhelmed me. – Ben May 03 '13 at 06:38
2

Edit:

From your comment:

extra_infoXXXXX holds a string

...it sounds like if subscid contains "foo", you want to get the value of extra_infofoo. If so, you'll need an object to look that up; otherwise, you'll be forced to use eval.

If these extra_infoxxxx variables are globals, you can look them up on window:

selector = window['extra_info' + subscid];

If not, I hate to say, you're stuck with eval:

selector = eval('extra_info' + subscid); // Blech

But note that if you're doing that, it's best to step back and reevaluate (no pun!) your design. For instance, perhaps you could make an object with the extra info as properties:

var extra_info = {
    foo: "bar"
};

Then you could look up the information like this:

selector = extra_info[subscid];

Original Answer:

It's very hard to tell from the information you've given, but I think you're looking for:

selector = extra_info[subscid];

...assuming that subscid contains the name of the property on extra_info that you want to access.

In JavaScript, you can access a property on an object using dotted notation and a literal property name:

x = foo.bar;

...or using bracketed notation and a string property name:

x = foo["bar"];

In the second case, the string can be the result of any expression. So for instance:

b = "bar";
x = foo[b];

or even

x = foo['b' + 'a' + 'r'];
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

To get the variabale do like this,

selector = extra_info+""+subscid['XXX']
  • extra_info is not an array, the name of the variable I need is extra_infoxxx where xxx is stored in subscid – Borniet May 03 '13 at 06:10
  • @Steve that was also my original answer, then Borniet commented that the other half of the variable was inside subscid and hence edited my answer. – SrikanthManian May 03 '13 at 06:29