retrieve current index while looping an array

  • Thread starter motion musso aka: sathia
  • Start date
M

motion musso aka: sathia

this is it, how can i get the current index? couldn't figure it out.

thank you

for(i=0;myarray.length<i;i++){
do_something();
//i need the current myarray[index]
}

bye bye
 
R

Randy Webb

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<i;i++){
do_something();
//i need the current myarray[index]

alert(myarray)
 
M

motion musso aka: sathia

Randy said:
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<i;i++){
do_something();
//i need the current myarray[index]

alert(myarray)


thank you, this is the pointer's value, i'll try to be more clear

myarray[0] = "a";
myarray[1] = "b";
myarray[2] = "c";
myarray[3] = "d";
myarray[4] = "e";
myarray[5] = "f";
myarray[6] = "g";

let's say i'm now having a:
 
M

motion musso aka: sathia

Randy said:
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<i;i++){
do_something();
//i need the current myarray[index]

alert(myarray)


thank you, this is the pointer's value, i'll try to be more clear

myarray[0] = "a";
myarray[1] = "b";
myarray[2] = "c";
myarray[3] = "d";
myarray[4] = "e";
myarray[5] = "f";
myarray[6] = "g";

let's say i'm now having a:
myarray.reverse();

for(i=0;myarray.length<i;i++){
do_something();
//i need the current myarray[index]
/*
now I want that my index is shown like:
6 5 4 3 2 1 0
*/

}


thank you
 
R

Randy Webb

motion musso aka: sathia said the following on 8/13/2006 12:38 PM:
Randy said:
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<i;i++){
do_something();
//i need the current myarray[index]
alert(myarray)


thank you, this is the pointer's value, i'll try to be more clear


Not entirely. myarray will give the value of myarray.
myarray[0] = "a";
myarray[1] = "b";
myarray[2] = "c";
myarray[3] = "d";
myarray[4] = "e";
myarray[5] = "f";
myarray[6] = "g";

let's say i'm now having a:
myarray.reverse();

for(i=0;myarray.length<i;i++){
do_something();
//i need the current myarray[index]
/*
now I want that my index is shown like:

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:

for(i=myarray.length;i>=0;i--){
do_something();
}

Or:

var theLength = myarray.length
while(theLength--){
do_something();
}
 
M

motion musso aka: sathia

Randy said:
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<i;i++){
     alert(my_other_array[myarray_index]);
}

bye
 
L

Lasse Reichstein Nielsen

motion musso aka: sathia said:
thank you, this is the pointer's value, i'll try to be more clear

myarray[0] = "a";
myarray[1] = "b";
myarray[2] = "c";
myarray[3] = "d";
myarray[4] = "e";
myarray[5] = "f";
myarray[6] = "g";

let's say i'm now having a:
myarray.reverse();

That is, your array is now equivalent to:
["g","f","e","d","c","b","a"]
for(i=0;myarray.length<i;i++){
do_something();
//i need the current myarray[index]

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?
/*
now I want that my index is shown like:
6 5 4 3 2 1 0

Use:
for (var i = myarray.length - 1; i >= 0; i--) {
for iterating ... but it sounds like you want something more
general.

/L
 
M

motion musso aka: sathia

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_
 
R

RobG

motion said:
Lasse Reichstein Nielsen wrote:



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<anArray.length; i++){
if ('banana' == anArray) {
alert('banana is at index ' + i);
}
}

Now if you do:

anArray.shift();

What is the index of 'banana' now? Arrays don't have to be contiguous:

anArray[100] = 'orange';


The length of anArray is now 101, even though it only has 4 elements
(or items or members, whatever). Such arrays are often called
'sparse'. You can iterate over just the indexes with a value using
for..in, but be aware that will find *all* enumerable properties of the
Array, not just the ones with numeric indexes.

Secondly, remember that Array's are just objects with a few extra
methods and a special length property. You can add properties to
ordinary objects and give them 'indexes', and you can add properties to
an array that don't have numeric indexes:

anArray.name = 'fred';

Now a for..in loop through anArray will stumble across 'fred' as well
as the four fruits, but looping over the indexes won't.

Also think about:

var anObject = { 0 : 'apple', 1 : 'banana', 2 : 'pear'};


How would you find the 'index' of banana now? Probably using a for..in
loop:

for (idx in anObject){
if ('banana' == anObject[idx]){
alert('Property name for banana is ' + idx);
}
}


I hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top