javascript - Traversing negative Array indeces in JScript -


i have sparse array in jscript, non-null elements occuring @ both negative , positive indices. when try use in loop, doesn't traverse array lowest (negative) index highest positive index. instead returns array in order added elements. enumeration doesn't work either. there method allow me that?

example

arrname = new array(); arrname[-10] = "a"; arrname[20] = "b"; arrname[10] = "c"; 

when looping through, should give me c b.

technically, "a" isn't in array @ since can't have negative index. member of arrname object. if check arrname.length see 21 (0,1,2,...,20) why don't use plain object instead (as hashtable). should work:

<script type="text/javascript"> //define , initialize object/hastable var obj = {}; obj[20] = 'c'; obj[10] = 'b'; obj[-10] = 'a';  // indexes , sort them var indexes = []; for(var in obj){     indexes.push(i); } indexes.sort(function(a,b){     return a-b; });  // write values page in index order (increasing) for(var i=0,l=indexes.length; i<l; i++){     document.write(obj[indexes[i]] + ' '); } // should print out "a b c" page </script> 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -