combining 2 multi-dim arrays

Discussion in 'Javascript' started by mike, Jan 12, 2006.

  1. mike

    mike Guest

    I have 2 multi-dimensional array I want to combine and I'm drawing a
    blank on the "how"

    var array1 = [
    ["1","a"],
    ["2","b"]
    ];

    var array2 = [
    ["3","c"],
    ["4","d"]
    ];

    These methods do not work:

    var array3 = [
    [array1],
    [array2]
    ];

    var array3 = [
    array1,array2
    ];

    array3 should look like this:

    ["1","a"],
    ["2","b"],
    ["3","c"],
    ["4","d"]

    Any help is appreciated.

    Mike
     
    mike, Jan 12, 2006
    #1
    1. Advertising

  2. "mike" <> writes:

    > I have 2 multi-dimensional array I want to combine

    ....
    > var array1 = [
    > ["1","a"],
    > ["2","b"]
    > ];
    >
    > var array2 = [
    > ["3","c"],
    > ["4","d"]
    > ];

    .....
    > array3 should look like this:
    >
    > ["1","a"],
    > ["2","b"],
    > ["3","c"],
    > ["4","d"]


    Try
    var array3 = array1.concat(array2);

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 12, 2006
    #2
    1. Advertising

  3. mike wrote:

    > I have 2 multi-dimensional array I want to combine and I'm drawing a
    > blank on the "how"
    >
    > var array1 = [
    > ["1","a"],
    > ["2","b"]
    > ];
    >
    > var array2 = [
    > ["3","c"],
    > ["4","d"]
    > ];
    >
    > These methods do not work:
    >
    > var array3 = [
    > [array1],
    > [array2]
    > ];


    That evaluates to

    var array3 = [[[["1","a"], ["2","b"]]], [[["3","c"], ["4","d"]]]];

    > var array3 = [
    > array1,array2
    > ];


    That evaluates to:

    var array3 = [[["1","a"], ["2","b"]], [["3","c"], ["4","d"]]];

    > array3 should look like this:
    >
    > ["1","a"],
    > ["2","b"],
    > ["3","c"],
    > ["4","d"]
    >
    > Any help is appreciated.


    Specific:

    var array3 = [array1[0], array[1], array2[0], array2[1]];

    General:

    var array3 = array1.concat(array2);

    (JavaScript 1.2 (NN4+), JScript 3.0 (IE4+), ECMAScript 3) or

    var
    a = [array1, array2],
    array3 = [];

    for (var i = 0, len = a.length; i < len; i++)
    {
    var o = a;
    for (var j = 0, len2 = o.length; j < len2; j++)
    {
    array3.push(o[j]); // or: array3[array3.length] = o[j];
    }
    }

    The following method helps to recognize that:

    Array.prototype.toString = function()
    {
    return "[" + this.join(",") + "]";
    }

    Examples:

    window.alert([array1, array2, array3].join("\n"));
    window.alert([array1, array2, array1.concat(array2)].join("\n"));


    HTH

    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 12, 2006
    #3
  4. mike

    mike Guest

    super ...

    I like this:

    var array3 = array1.concat(array2);

    it works fine.

    Mike
     
    mike, Jan 12, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Palas
    Replies:
    1
    Views:
    506
  2. Florian Kaufmann
    Replies:
    1
    Views:
    381
  3. Jack

    Allocating multi-dim'l arrays

    Jack, Jul 12, 2011, in forum: C Programming
    Replies:
    7
    Views:
    431
    Keith Thompson
    Jul 13, 2011
  4. Gijs Nijholt

    multi dim array?

    Gijs Nijholt, Oct 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    108
    Robert Klemme
    Oct 20, 2006
  5. Domenico Discepola

    Deleting and moving multi-dim array "columns"

    Domenico Discepola, Dec 23, 2003, in forum: Perl Misc
    Replies:
    7
    Views:
    90
    pkent
    Dec 23, 2003
Loading...

Share This Page