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 3 of 3

Thread: Merge 2 Arrays

  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Merge 2 Arrays

    Hi,

    I am trying to merge the values of 2 arrays.

    array 1 = 1, 2, 3, 4, 5

    array 2 = a, b, c, d, e

    Resulting in:

    array 3 = 1-a, 2-b, 3-c, 4-d, 5-e etc

    both arrays that will be used originally will always of the same length. Both arrays will contain numbers

    Can you please help?

    Thanks

  • #2
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can't seem to find much in Jquery that will do this for me.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You say "both arrays will contain numbers" but a,b,c,d,e are strings. I am assuming that the numbers in your first array are in fact string values.
    Code:
    <script type = "text/javascript">
    
    var arr1 = ["1", "2", "3", "4", "5"];
    var arr2 = ["a","b","c","d","e"];
    var arr3 = [];
    var len = arr1.length;
    for (var i = 0; i<len; i++) {
    arr3[i] = arr1[i] + "-" + arr2[i];
    }
    document.write (arr3);
    
    </script>

    Quizmaster: The phrase "Workers of the world unite" is inscribed on the tomb of which political thinker in Highgate Cemetery, north London?
    Contestant: Enoch Powell.

    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.


  •  

    Posting Permissions

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