5

Is there a way to define variables in javascript to append them with another variable?

var card_id;
var card_links_grid+card_id;

I'd like the second variable to be appended with the information found in the first variable. For each card_id there will be a card_links_grid. I want to be able to access the card_links_grid variable for a specific card_id.

What's the right way to do that?

3
  • 5
    Normally when I think I need to change a variable name like this, I realise the easiest thing is to use an array instead Commented Nov 24, 2011 at 18:00
  • is this what you want? card_id=3 card_links_grid1 card_links_grid2 card_links_grid3 are 5,6 and 7 respt but when you card_links_grid+card_id you want it to return 7?? am i correct? Commented Nov 24, 2011 at 18:03
  • An object will do what I am looking for. Commented Nov 24, 2011 at 20:23

3 Answers 3

8

The right was is to use either an array or an object, depending on if the id is going to be sequential or named.

var card_links_grid = {};
var card_id = "the_hanged_man";
card_links_grid[card_id] = "some value";

or

var card_links_grid = [];
card_links_grid.push("some value");

Variable variables are possible, but only with some hard to maintain and debug approaches that should be avoided. Use object types designed for the data structure you want to represent.

Sign up to request clarification or add additional context in comments.

3 Comments

I agree, it sounds like you should use an object. The above example should solve your problem.
@Quentin Your suggestion is what I am looking for. I don't really have to append to the variable, since the object will allow me to retrieve information from card_links_grid with the card_id. I am testing this approach.
An object worked. I have var card_id = "an id"; var anobject = new object; var card_links_grid = {}; card_links_grid[card_id] = anobject; This does what I need. Thanks.
3

I also recommend to use either array or object. But if you want to try with variable variable approach, you can try like this.

var card_id=1;
window['card_links_grid_' + card_id] = 'Marco';
document.write(window['card_links_grid_' + card_id]);

http://jsfiddle.net/Q2rVH/

Ref: http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri

1 Comment

I like the idea of an object.
0

you could use the eval method for the same and get away with it.. something like this

var card_id=3;

var card_links_grid1=5,card_links_grid2=6,card_links_grid3=7;

eval("card_links_grid"+card_id) //will  o/p 7

P.S: If I ustood the question correctly

2 Comments

You could, but it is slow and hard to debug. Use a proper data structure instead. There is almost never any reason to use eval.
agreed! like I am all for using an associative array.. but I am answering to the question and nt suggesting an alternative.. its like people suggesting use jquery for most js questions here! zzzz..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.