www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: How to convert array to arguments array

  1. #1
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68

    How to convert array to arguments array

    Hi everyone

    I want to pass an array of unknown length to a function, in the way that every element of the array is received as an argument:

    var arrTest = [1,4,6,0,"bla"];

    function Test() {
    alert(arguments.length);
    }

    Test(arrTest.toString());

    outputs 1, instead of 5!!!

    I could do:

    Test(arrTest[0],arrTest[1],...) but the problem is I don't know the lenght of the array in advance.

    Any suggestions?
    Simon
    Snow Crash, www.speich.net

  2. #2
    Join Date
    Jun 2003
    Location
    Virginia
    Posts
    675
    Maybe better off is to make this array an "array of arrays"
    (JS's approach to a multi-dim array). Then, each element
    would have a "name" [0] and "value" [1] pair.
    PHP Code:
    <script type="text/javascript">
    function 
    testIt() {
      var 
    str="";
      var 
    myArray = new Array();
      var 
    colorArray = new Array("Green","Red","Blue");

      for (var 
    i=0i<tempArray.lengthi++) {
        var 
    temp = new Array();
        
    temp[0] = i+10;
        
    temp[1] = colorArray[i];
        
    myArray[i] = tempj;
      }

      for (var 
    i=0i<myArray.lengthi++) {
        
    str += myArray[i][0] + " " myArray[i][1] + "\n";
      }
      
    alert("Array results\n" str);
    }
    </script> 

  3. #3
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    Sorry, but I don't see how this will help me in solving my problem to get

    arguments.length == 5 (=arrTest.length)

    arguments[0] == 1
    arguments[1] == 4
    arguments[2] == 6
    arguments[3] == 0
    arguments[3] == "bla"

    after passing arrTest to the Test() function.
    Snow Crash, www.speich.net

  4. #4
    Join Date
    Nov 2002
    Location
    Nashua, NH
    Posts
    3,195
    What's wrong with passing the whole array as an argument???
    Vladdy

    Working web site is not the one that looks the same in a few graphical browsers, but the one that adequately delivers its content to any device accessing it.

  5. #5
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    Nothing,


    Test(arrTest); doesn't help either

    I just want to be able to access every element in the passed array through the arguments array. You might ask why don't you use the passed array directly instead of the arguments array. The reason is, that I use the same function in another context where the function ist used this way:

    Test(23, 345, 2 ,3);

    and then I use the arguments array to access the passed variables.

    I would have to check if the first argument is an array (with typeof) and then fork the whole thing and write a special part only for the array. I would be easier if I just could pass every value of the array to the function such as

    Test(arrTest[0], arrTest[1], arrTest[2],..) but I don't know the length of arrTest.
    Last edited by SnowCrash; 03-10-2004 at 10:44 AM.
    Snow Crash, www.speich.net

  6. #6
    Join Date
    Nov 2002
    Location
    Nashua, NH
    Posts
    3,195
    calling the function in another context as:
    Test(new Array(23, 345, 2, 3));
    solves the problem with much less effort and headache.
    Vladdy

    Working web site is not the one that looks the same in a few graphical browsers, but the one that adequately delivers its content to any device accessing it.

  7. #7
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    Yep, you're right, why not doing it the easy way. But I'm curious if there is not a way to make it work (that's why i used the toString()) with somethin like:

    Test(eval(arrTest.toString()));


    But thanks, I'll do it your way...
    Snow Crash, www.speich.net

  8. #8
    Join Date
    Jun 2003
    Location
    Virginia
    Posts
    675
    What is the structure of this function right now. It apparently
    accepts string/number variables and not of type array().
    How many variable arguments are there right now? It seems
    the best course is to re-write it so that it only accepts an
    array. Maybe this is what vladdy has already mentioned.

    I'm confused.

  9. #9
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    I didn't mean to confuse you. I will do what Vladdy's suggested, e.g. only pass arrays to the function and then instead of working with the arguments array I work with the passed array. But if you want to know what I was talking about:

    I have a function that receives a unknown number of arguments which are normally just numbers (would work with strings as well) for example

    Test(2,10,56,4);

    inside the function I access the passed numbers:

    Test() {
    while (i < arguments.length) {
    alert(arguments[i]);
    i++;
    }

    but if I pass an array

    Test(arrTest);

    then arguments.length = 1 and arguments[0] holds the passed array.

    But what I wanted is that the array doesn't get passed as an array but as single comma separated values so as the while loop would loop through all elements of the array.
    That's why I tried to do

    Test(arrTest.toString());

    Now with Vladdy's suggestion I replace the arguments loop with the passed array and pass the single numbers also as an array:
    e.g.

    Test(new Array(2,10,56,4));


    I hope this was clearer.
    Simon
    Last edited by SnowCrash; 03-10-2004 at 11:23 AM.
    Snow Crash, www.speich.net

  10. #10
    Join Date
    Jun 2003
    Location
    Virginia
    Posts
    675
    I have a function that receives a unknown number of arguments
    I guess this phrase is whats throwing me. Correct me, if you
    like, so I understand what you mean. It sounds as if you
    have an unknown amount of values (delimited by commas)
    contained in "one" variable argument.

    So the original function structure appeared something like??
    function test(val) {
    }

  11. #11
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    It sounds as if you
    have an unknown amount of values (delimited by commas)
    correct

    contained in "one" variable argument.
    wrong. Every value is passed as an argument as shown in the example in my previous posts (also shown below)

    PHP Code:
     Test(2,10,56,4);

    Test() {
      while (
    arguments.length) {
      
    alert(arguments[i]);
      
    i++;

    Snow Crash, www.speich.net

  12. #12
    Join Date
    Jun 2003
    Location
    Virginia
    Posts
    675
    OK - I'm stupid. Not familiar with that type of structure. Of
    course, this appears as if JS is "implicitly" handling
    the passed values as an Array, contained in the "arguments"
    object.

    So to clear my mind up, you are now going to "explicitly"
    prepare the function to accept an Array() argument??

  13. #13
    Join Date
    Jun 2003
    Location
    The United States Site: http://kellyj.t35.com
    Posts
    2,561
    you are only sending one argument if you send an array. The array however has the length of what you need. You could do a simple .split:
    PHP Code:
    function split(val) {
      var 
    myVals val.split(',')

    That would take the array you sent as a string and create it back into an array.Then of course myVals.length would be what you want, not arguments.length

  14. #14
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    in reply to olerag

    My problem is, that I do not always call the function Test() in the way:

    Test(2,10,56,4);

    but also in the way:

    var arrTest = new Array(2,10,56,4);
    Test(arrTest);

    and then arrTest is seen only as 1 argument in the Test() function (arguments.length = 1) , but I wanted the while loop to output all the elements in the array.

    As Vladdy pointed out instead of trying the arguments object to accept each element of the array as an argument I should just rewrite the Test() function:
    PHP Code:
    function Test(arrTest) {
      for (var 
    0arrTest.lengthi++)
        
    alert(arrTest[i]);

    and always pass an array:

    Test(new Array(2,10,56,4));
    Last edited by SnowCrash; 03-10-2004 at 02:15 PM.
    Snow Crash, www.speich.net

  15. #15
    Join Date
    Oct 2003
    Location
    Switzerland
    Posts
    68
    This solution doesn' really make me happy, because I use the Test() function severall times and I don't want to create every time a new array object that I only use once. So I think about using typeof to check if the first argument is an array or not and then...

    ... is to come or any suggestions ?
    Snow Crash, www.speich.net

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles