Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  60] [ 1]  / answers: 1 / hits: 17636  / 13 Years ago, thu, november 24, 2011, 12:00:00

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?


More From » variables

 Answers
68

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.


[#88916] Wednesday, November 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gordonrockym

Total Points: 95
Total Questions: 115
Total Answers: 95

Location: Iraq
Member since Sat, Apr 16, 2022
2 Years ago
;