Connecting Tech Pros Worldwide Forums | Help | Site Map

passing an array element to a function

csx
Guest
 
Posts: n/a
#1: Jul 20 '05
Hi all,
Here's a simple problem I'm having problems with. How do I pass an array
element to a function.

For instance,

values[0] = new values(1, 1,'A',0);
values[1] = new values(2, 2,'B',1);
values[2] = new values(3, 3,'C',2);


Tto call the function:
var cols = count(values[1]);

The reciving function:
function count(values_array[n])

I need to pass an array element, rather than the whole array. Its a
recursive function, so i need to specifically keep passing back into the
function, the actual array index.

Thanks in advance!!



Ivo
Guest
 
Posts: n/a
#2: Jul 20 '05

re: passing an array element to a function


"csx" <csx@ms.com> wrote in message
news:blfjii$ibk$1@hercules.btinternet.com...[color=blue]
> Hi all,
> Here's a simple problem I'm having problems with. How do I pass an array
> element to a function.
>
> For instance,
>
> values[0] = new values(1, 1,'A',0);
> values[1] = new values(2, 2,'B',1);
> values[2] = new values(3, 3,'C',2);[/color]

Running your code gave me this fatal error: 'values' is undefined.
The bit after the word "new" is expected to be an object.
"Array" is a built-in object, "values" is not.
You are looking for this syntax:

values=new Array(); // should precede the first values[n]
values[0]=new Array( 1, 1, 'A', 0 );
values[1]=new Array( 2, 2, 'B', 1, new Array( 4, '4,5', 5 ) );
values[2]=[ values[1][4] ,'hello', world ];

alert( values ); // Array 1,1,A,0,2,2,B,1,4,4,5,5
alert( values[0] ); // Array 1,1,A,0
alert( values[0][2] ); // A
alert( values[1][2] ); // B
alert( values[1][4][1] ); // 4,5
alert( values[2][0]); // Array 4,4,5,5 // !
alert( values[2][2]); // error: 'world' is undefined

The square brackets used to initiate values[2] are a short-hand way of
saying "new Array". They 're not as well supported across browsers but quite
logical if you get used to them.
HTH
Ivo


W d'Anjos
Guest
 
Posts: n/a
#3: Jul 20 '05

re: passing an array element to a function


values[1].length gives you the number of elements in the array.

values[1][0] addresses a specific element

To call the function:
var cols = count(values[1][0]);

The reciving function:
function count(e)


I hope this helps,

Wagner


"csx" <csx@ms.com> wrote in message news:<blfjii$ibk$1@hercules.btinternet.com>...[color=blue]
> Hi all,
> Here's a simple problem I'm having problems with. How do I pass an array
> element to a function.
>
> For instance,
>
> values[0] = new values(1, 1,'A',0);
> values[1] = new values(2, 2,'B',1);
> values[2] = new values(3, 3,'C',2);
>
>
> Tto call the function:
> var cols = count(values[1]);
>
> The reciving function:
> function count(values_array[n])
>
> I need to pass an array element, rather than the whole array. Its a
> recursive function, so i need to specifically keep passing back into the
> function, the actual array index.
>
> Thanks in advance!![/color]
Closed Thread


Similar JavaScript / Ajax / DHTML bytes