passing an array element to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csx

    passing an array element to a function

    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_ar ray[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

    #2
    Re: passing an array element to a function

    "csx" <csx@ms.com> wrote in message
    news:blfjii$ibk $1@hercules.bti nternet.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


    Comment

    • W d'Anjos

      #3
      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$ib k$1@hercules.bt internet.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_ar ray[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]

      Comment

      Working...