Question:
Javascript Array Question - Current Index?
Ed W
2007-09-20 17:08:11 UTC
I have a JavaScript array that constantly changes size because users are able to dynmaically add/remove items without leaving or refreshing the page. Here's the problem, I want to know the index number of each item in the array.

Example:


The tricky part is that there may be 1 of these buttons, there may be 10, they all have the same name/id and users can add/remove them without ever refreshing the page, so how do I find <> ??

Any help would be GREATLY appreciated, I cannot figure this one out.
Five answers:
§©®Î¶†Δ®
2007-09-21 10:06:08 UTC
The buttons don't really matter, but as stated above, each form element should have its own unique ID. It's okay to have the same "name" (not ID) for certain form elements such as the input type="radio" element.



One thing you can do is add all new array elements to the end of the array and use the array's ".length" property to find out what its index is (just subtract 1 from the length to get its index.) Perhaps you can store that index in another keyed array for easy lookup. You can keep a parallel array that is always sorted (look up the sort(), splice(), push(), and pop() functions) and then you can do a binary search for random access to the master array. Just make sure you update both arrays when you make changes to the master array. This technique is fast but it uses more memory.



Yet another thing you can do (this was already suggested) is use object literal syntax and access your "array" as a hash table. If you really wanted an index, you could store the index into your master array as a value of your keyed hash table. This is pretty fast as well.



Finally, and I don't suggest this, you can do a sequential search of the array for the element you're looking for using a "for" loop. Slow, but uses no extra memory (memory is cheap, though.)
oechsle
2016-09-28 09:08:01 UTC
Javascript Array Index
richarduie
2007-09-20 17:33:13 UTC
Same id is not a good thing...confuses the DOM. JavaScript supports sparse arrays, i.e., doesn't require that index values be populated sequentially or completely; it also allows "indexes" that are objects (associative array, kind of a Hashtable thing). Here's a simple example, using the associative array approach.
















onclick="registerButton(this);whateverElseOnClick(this);"

value="button 1" />


onclick="registerButton(this);whateverElseOnClick(this);"

value="button 2" />


onclick="registerButton(this);whateverElseOnClick(this);"

value="button 3" />





J P
2007-09-20 17:16:31 UTC
First of all, an id should be unique that is the whole purpose behind having an id. Secondly, you didn't say anything about how items get added to the array or how they are indexed, so I can't really answer your question without more information.
thakkar k
2007-09-20 22:36:51 UTC
var myArr = new Array();



myArr[0] = "Alpha";

myArr[1] = "Beta";

myArr[2] = "Gamma";



alert(myArr[0]); // alerts 'Alpha'

alert(typeof myArr[0]); // 'string'

alert(myArr.length); // alerts '3'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...