By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

passing an array element to a function

P: n/a
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!!
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ivo
"csx" <cs*@ms.com> wrote in message
news:bl**********@hercules.btinternet.com...
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
Jul 20 '05 #2

P: n/a
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" <cs*@ms.com> wrote in message news:<bl**********@hercules.btinternet.com>...
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!!

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.