Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  93] [ 1]  / answers: 1 / hits: 38672  / 11 Years ago, mon, may 20, 2013, 12:00:00

Is it possible to set the custom index of an array with a variable.



for example:



var indexID = 5;
var temp = {
indexID: new Array()
};


The above example sets the array index to indexID and not 5. I have tried using = snd quotes but I without any success.



Thnaks


More From » arrays

 Answers
9

Yes, just use square brackets notation:



var temp = {};
temp[indexID] = [];


Also pay attention to the fact that temp is an object, and not an array. In JavaScript all associative arrays (or dictionaries) are represented as objects.



MORE: http://www.jibbering.com/faq/faq_notes/square_brackets.html#vId


[#78125] Saturday, May 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;