464,406 Members | 1,351 Online
Need help? Post your question and get tips & solutions from a community of 464,406 IT Pros & Developers. It's quick & easy.

# retrieve current index while looping an array

 P: n/a this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length Aug 13 '06 #1
11 Replies

 P: n/a motion musso aka: sathia said the following on 8/13/2006 10:50 AM: this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length

 P: n/a Randy Webb wrote: motion musso aka: sathia said the following on 8/13/2006 10:50 AM: >this is it, how can i get the current index? couldn't figure it out.thank youfor(i=0;myarray.length Aug 13 '06 #3

 P: n/a Randy Webb wrote: motion musso aka: sathia said the following on 8/13/2006 10:50 AM: >this is it, how can i get the current index? couldn't figure it out.thank youfor(i=0;myarray.length Aug 13 '06 #4

 P: n/a motion musso aka: sathia said the following on 8/13/2006 12:38 PM: Randy Webb wrote: >motion musso aka: sathia said the following on 8/13/2006 10:50 AM: >>this is it, how can i get the current index? couldn't figure it out.thank youfor(i=0;myarray.length=0;i--){ do_something(); } Or: var theLength = myarray.length while(theLength--){ do_something(); } -- Randy comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ Aug 13 '06 #5

 P: n/a Randy Webb wrote: I think what you want is to loop through myarray in reverse but know where the value was at in the original array. In that case, you simply loop through the array backwards: thank you again, my point is that i need to know which index i'm using at the moment because i want to get the value of a second array for(i=0;myarray.length Aug 13 '06 #6

 P: n/a "motion musso aka: sathia"

 P: n/a NO wrote: alert(i); ok, now I'm sure I explained myself plainly wrong. Anyway you can see my needs on the other answer I gave. thank you -- una volta qui era tutto Aug 13 '06 #8

 P: n/a "motion musso aka: sathia" = 0; i--) { for iterating ... but it sounds like you want something more general. /L -- Lasse Reichstein Nielsen - lr*@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.' Aug 13 '06 #9

 P: n/a Lasse Reichstein Nielsen wrote: Index of what? If the variable "i" is 0, do you want the index of "a" in myarray? If so, how do you find "a" from the value 0, if you don't have the original value of myarray any more? I got it now, I thought that when you give indexes to an array they would remain unvaried. probably this is true only with string indexes. thank you for your patience Sat_ -- una volta qui era tutto Aug 13 '06 #10

 P: n/a motion musso aka: sathia wrote: Lasse Reichstein Nielsen wrote: Index of what? If the variable "i" is 0, do you want the index of "a" in myarray? If so, how do you find "a" from the value 0, if you don't have the original value of myarray any more? I got it now, I thought that when you give indexes to an array they would remain unvaried. probably this is true only with string indexes. Read the ECMAScript Language reference (section 15.4) - array indexes *are* strings: "Array objects give special treatment to a certain class of property names. A property name P (in the form of a string value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32-1." I think you are getting confused between Object objects and Array objects. The only way to find the index of a particular value in an array is to search for it, e.g.: var anArray = ['apple', 'banana', 'pear']; What is the index of 'banana'? for (var i=0; i

 P: n/a RobG wrote: I hope that helps. this is just perfect. thank you very much for your time Sat_ -- una volta qui era tutto Aug 14 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.