473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

retrieve current index while looping an array

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

--
una volta qui era tutto <table>
Aug 13 '06 #1
11 6665
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[i])

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 13 '06 #2
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 you

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

alert(myarray[i])
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:

--
una volta qui era tutto <table>
Aug 13 '06 #3
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 you

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

alert(myarray[i])
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

--
una volta qui era tutto <table>
Aug 13 '06 #4
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 you

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

thank you, this is the pointer's value, i'll try to be more clear
Not entirely. myarray[i] will give the value of myarray[i].
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();
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 13 '06 #5
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<i;i++){
* * *alert(my_other_array[myarray_index]);
}

bye

--
una volta qui era tutto <table>
Aug 13 '06 #6
"motion musso aka: sathia" <sa**********@libero.itwrote in news:l%
GD*******************@twister1.libero.it:
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(i);
Aug 13 '06 #7
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 <table>
Aug 13 '06 #8
"motion musso aka: sathia" <sa**********@libero.itwrites:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 13 '06 #9
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 <table>
Aug 13 '06 #10

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<anArray.length; i++){
if ('banana' == anArray[i]) {
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.
--
Rob

Aug 13 '06 #11
RobG wrote:
I hope that helps.
this is just perfect.

thank you very much for your time

Sat_
--
una volta qui era tutto <table>
Aug 14 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: forums_mp | last post by:
I've got an STL class (see below) with two functions to store and retrieve data - msg structs. The "Store" function when called will copy the received message (depending on which message) into...
7
by: Magnus Warker | last post by:
Hi, I want to traverse an (associative) array, starting from its current position, until a certain element is reached. Then, in certain cases, I want to be able to reset the current position of...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
2
by: JW | last post by:
Dear NG, I am writing a .NET CF forms app. that needs to dynamically configure buttons on a form. Since the forms designer doesn't like component arrays I am using ordinary instance names for...
4
by: midlothian | last post by:
Hello Is there a way to get the index of an item in an array? For instance, if I have: MyArray = Array("NY","CT","TX","NM") ....can I see what position CT is in the array? Thanks
4
by: BravoFoxtrot | last post by:
Hi, I'm trying to build an index into a multi dimensional associative array. I may not know how many dimensions there are so i want to pass the array indexes as a variable. $arrayToAccess =...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
3
by: Keith Thompson | last post by:
Victor <vhnguyenn@yahoo.comwrites: You're declaring an array of pointers to unsigned long long, but you're initializing the pointers with integer values. This is actually a constraint...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.