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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Help Array!!!!!

  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help Array!!!!!

    Hello guy! I have a problem with array, i don't know how to build a new array using the numbers of other array!
    EX: I have
    array1 = [1, 4, 2, 13, 5]
    with this array1 I want to build other array2 using the numbers in the first array. So i don't know how to take number from array and put it in an other array!
    Thanks all

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    See JavaScript Array Object on the site w3scholls.com and particularly the methods(*) concat(), slice() or splice()...

    (*) See too the examples of each method.
    Last edited by 007julien; 12-29-2012 at 04:19 PM.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    Several methods,

    .push()
    .slice()
    .splice()
    .concat()

    And there may be more I have not thought of just off the cuff.

    Dang, I type too slow.
    Last edited by jmrker; 12-29-2012 at 04:21 PM. Reason: Hunt and peck on tablet.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    var array1 = [1, 4, 2, 13, 5];
    var array2 = [];
    for var (i=0; i<array1.length; i++) {
    array2[i] = array1[i];
    }
    When you are playing all day, every year, it makes a difference. - Football team coach

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks so much at all!!!
    Philip Supreme Master

  • #6
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    var array1 = [1, 4, 2, 13, 5];
    var array2 = [];
    for var (i=0; i<array1.length; i++) {
    array2[i] = array1[i];
    }
    When you are playing all day, every year, it makes a difference. - Football team coach
    But if I want put in array2 only the even or odd number???
    How to do???

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by triko View Post
    But if I want put in array2 only the even or odd number???
    How to do???
    What does that mean? Do you mean only the odd/even indices (1,3,5 etc. or 0,2,4 etc.) or do you mean populate the second array only with numbers in the first array which are odd/even? (1,13,5 are odd in your example).

    Surely you can guess that this would involve an if...else statement.

    if (array1[i]%2 == 0) // value is even

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes using your first method, im solve alone but now i have an other problem
    All code is write right but, remainder is NaN and evenOdd don't have value of "i"
    Code:
    function exchangeSymmetric ()
    {
        var arrayPrincipal = [4, 2, 13, 0, 5];
        var array1 = [];
        var array2 = [];
        for (i = 0; i < arrayPrincipal.length; i++)
        {
            var evenOdd = array1 [i];
            var remainder = evenOdd%2;
        }
        alert (remainder);
    }

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Question

    Quote Originally Posted by triko View Post
    Yes using your first method, im solve alone but now i have an other problem
    All code is write right but, remainder is NaN and evenOdd don't have value of "i"
    Code:
    function exchangeSymmetric ()
    {
        var arrayPrincipal = [4, 2, 13, 0, 5];
        var array1 = [];
        var array2 = [];
        for (i = 0; i < arrayPrincipal.length; i++)
        {
            var evenOdd = array1 [i];
            var remainder = evenOdd%2;
        }
        alert (remainder);
    }
    Your code makes no sense to me.
    You create an arrayPrinciple and use the length in a for...loop,
    but you never check its contents.

    When you make an assignment to the evenOdd variable,
    you are using an element position in an empty array1 ... hence the NaN error.

    You save the last remainder of the tests which is what becomes alerted,
    but it is outside the for...loop which is why only one alert occurs.

    What is it that you are really trying to accomplish?

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by triko View Post
    Hello guy! I have a problem with array, i don't know how to build a new array using the numbers of other array!
    EX: I have
    array1 = [1, 4, 2, 13, 5]
    with this array1 I want to build other array2 using the numbers in the first array. So i don't know how to take number from array and put it in an other array!
    Thanks all
    A simple way to make a copy of an array without needing a loop.

    Code:
    array1 =  [1, 4, 2, 13, 5];
    array2 = [].concat(array1);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    When you make an assignment to the evenOdd variable,
    you are using an element position in an empty array1 ... hence the NaN error.

    What is it that you are really trying to accomplish?
    Yes You have reason... no sense, i'm wake up this morning and my brain asked me the answer This is the final problem
    Code:
    function exchangeSymmetric ()
    {
        var arrayPrincipal = [4, 2, 13, 0, 5];
        var arrayEven = [];
        var arrayOdd = [];
        for (i = 0; i < arrayPrincipal.length; i++)
        {
            var evenOdd = arrayPrincipal [i];
            var remainder = evenOdd%2;
            if (remainder == 0)
                arrayEven [i] = arrayPrincipal [i];
            else
                arrayOdd [i] = arrayPrincipal [i];
        }
        alert ("This is Array Even" + " " + arrayEven);
        alert ("This is Array Odd" + " " + arrayOdd);
    }
    This is work!
    Only one problem, between the number that is the " comma " " , " !!! Don t know how to remove!!!!

  • #12
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    A simple way to make a copy of an array without needing a loop.

    Code:
    array1 =  [1, 4, 2, 13, 5];
    array2 = [].concat(array1);
    Yes fegal, I have test, but don't is this my problem, I have explained bad!!!

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by triko View Post
    Only one problem, between the number that is the " comma " " , " !!! Don t know how to remove!!!!
    Code:
    alert("This is ArrayEven " + arrayEven.join(" ").replace(/\s+/g, " "));

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #14
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    The default display of an array contents is with ',' separators.
    You can alter this display with a .join() as in the following example.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    var arrayPrincipal = [4, 2, 13, 0, 5];
    var str = '';
    str += arrayPrincipal+'\n\n'+arrayPrincipal.join(',')+'\n\n';
    str += arrayPrincipal.join(' ')+'\n\n'+arrayPrincipal.join(', ');
    alert(str);
    </script>
    </head>
    <body>
    
    </body>
    </html>

  • #15
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by triko View Post
    But if I want put in array2 only the even or odd number???
    How to do???
    The .slice method generates a new array from an existing one.
    For recent browsers that support the .filter method, you can just do:

    Code:
    <script type="text/javascript">
    
    array1 = [ 1, 4, 2, 13, 5 ];
    
    odds = array1.filter( function( elem ){ return elem % 2; } )
    
    alert( odds );
    
    </script>
    Last edited by Logic Ali; 12-31-2012 at 04:45 AM.

  • Users who have thanked Logic Ali for this post:

    jmrker (12-31-2012)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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