Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Old Pedant

Thread: Array functions

  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Array functions

    var arr=[];
    function abc()
    {
    var foo[]=xyz(arr);
    }
    function xyz()
    {
    for(var i=0;i<5;i++)
    {
    arr.push(i);
    }
    }

    How can I get the function xyz() results into array variable foo?
    Thanks for any help!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Lots of ways. But the first thing to do would be to decide if you really NEED that global array arr.

    If not, then keep it simple:
    Code:
    function abc()
    {
        var foo = xyz();
    }
    function xyz()
    {
        var arr = []; // local variable
        for(var i=0;i<5;i++)
        {
            arr.push(i);
        }
        return arr;
    }
    If you do need the global, then:
    Code:
    var arr = [];
    function xyz()
    {
        var arr = [];
        for(var i=0;i<5;i++)
        {
            arr.push(i);
        }
    }
    function abc()
    {
        // if not invoked from anyplace else, call xyz() first!
        if (arr.length === 0) xyz( );
        var foo = arr;
    }
    Now, the problem with this answer is that you really have two references to the one array. If you need foo to be an independent array that you can make changes to without affecting arr, then you need to clone the array:
    Code:
    var arr = [];
    function abc()
    {
        if (arr.length === 0) xyz( );
        var foo = arr.slice(0);
    }
    function xyz()
    {
        var arr = [];
        for(var i=0;i<5;i++)
        {
            arr.push(i);
        }
    }
    Javascriptdev likes this.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Javascriptdev (08-05-2014)

  • #3
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks Old Pedant!
    That was a clear explanation.
    But yeah, I do need the variable to be global so that I can save the 'xyz()' array results into 'foo' variable.
    If I keep it as a local variable, I am saving null or undefined values into 'foo'..

    And also, in the below function why do I need to arr.length===0 before calling xyz?
    function abc()
    {
    // if not invoked from anyplace else, call xyz() first!
    if (arr.length === 0) xyz( );
    var foo = arr;
    }

    I tried the code with global variable and also the slice method but both of the cases I am not able to retrieve the array results into 'foo'.
    When I debug, iam seeing no results in foo. what could be the reason?

    Thanks
    Last edited by Javascriptdev; 08-05-2014 at 03:00 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    SORRY! Too much copy, not enough cut. The var arr in the xyz() function was bogus!
    Code:
    function xyz()
    {
        var arr = []; // this line must be removed!
        for(var i=0;i<5;i++)
        {
            arr.push(i);
        }
    }
    Here is a working example. It shows you can modify the foo array without disturbing the arr array.
    Code:
    <script>
    var arr = [];
    function abc()
    {
        if (arr.length === 0) xyz( );
        var foo = arr.slice(0);
        foo[2] = "zamboni";
        document.write("foo: " + foo + "<hr/>\n");
    }
    function xyz()
    {
        for(var i=0;i<5;i++)
        {
            arr.push(i);
        }
    }
    abc( );
    document.write("arr: " + arr + "<hr/>\n");
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Javascriptdev (08-06-2014)

  • #5
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thank you so much..
    I have one more question:
    What if I want to use the foo array to check a condition using if.
    Can I do something like this?

    var arr = [];
    function abc()
    {
    var user='aaaa';
    if (arr.length === 0) xyz( );
    var foo = arr.slice(0);

    if(user==foo)//assuming 'aaaa' is one of the foo array users.
    {
    //some steps..
    }
    else
    {
    //some other steps..
    }
    }
    function xyz()
    {
    for(var i=0;i<5;i++)
    {
    arr.push(i);
    }
    }

    Is this correct?

    Thanks

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Code:
        if( foo.indexOf(user) >= 0 )//assuming 'aaaa' is one of the foo array users.
        {
            //some steps..
        } else {
            //some other steps..
        }
    Don't you have some JavaScript reference manual or equivalent handy??? Any such manual would show you the methods available on the Array object.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Javascriptdev (08-06-2014)


  •  

    Posting Permissions

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