passing an array element to a function

C

csx

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!!
 
I

Ivo

csx said:
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);

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

W d'Anjos

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
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top