Initialize multi-dimensional array

Discussion in 'Javascript' started by Archos, Jan 24, 2012.

  1. Archos

    Archos Guest

    I want to initialize a multidimensional array to a determined value,
    i.e. an array of dimensions [2][4] initialized to 1.

    I can do:
    var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
    Array(4); }
    for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
    [i1]=1; }}

    or shorter:
    var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
    Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

    But is there any another way to do it shorter? Without use `var a2 =
    [[1,1,1,1], [1,1,1,1]]`

    Thanks in advance!
     
    Archos, Jan 24, 2012
    #1
    1. Advertisements

  2. var arr = new Array ( new Array(1,2,3,4), new Array(2,4,6,8), new Array
    (3,6,9,12) );

    Rgds

    Denis McMahon
     
    Denis McMahon, Jan 25, 2012
    #2
    1. Advertisements

  3. What do you mean with "shorter"? Less code? Faster execution? If you
    don't want to use array literals, there will always be two loops:

    var a=[];for(var i=0;i<2;i++){a=[];for(var j=0;j<4;j++)a[j]=1;}

    There might be tricky expressions with less code, but you don't really
    want them.

    Bergi
     
    Andreas Bergmaier, Jan 25, 2012
    #3
  4. Archos

    Archos Guest



    I mean less code, and using simple '[]' is saved some space, but is it
    implemented in the main browsers or I could have any problem by its
    use?

    Which has a faster execution (array literal or '[]')?
     
    Archos, Jan 25, 2012
    #4
  5. Why, what is the problem with having more code?
    Array literals where formalised in ECMA 262 3rd Ed at the end
    of 1999, so has been around for long enough to be universally
    implemented in web browser's javascript engines.
    [] is an array literal; a literal declaration for an empty array.

    Richard.
     
    Richard Cornford, Jan 25, 2012
    #5
  6. Use an Array Comprehension.
     
    Michael Haufe (TNO), Jan 25, 2012
    #6

  7. ^^ ^^
    JFYI: You are using two Array literals here, of course.
    A matter of opinion. Surely

    var a = [];
    a.length = 5;
    a = a.join("1").split("").map(function (el) { return +el; });
    a = [a, a.slice()];

    is a more elegant solution to this problem.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 25, 2012
    #7
  8. In comp.lang.javascript message <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b2
    3g2000yqn.googlegroups.com>, Tue, 24 Jan 2012 15:09:00, Archos
    Slightly. Use [] instead of new Array(n). A while loop counting down
    is probably shorter & faster than a for loop counting up, and you can
    combine the decrement and the "was that zero?" test.

    var X, j, k, A = []
    j = 2 ; do { A[--j] = X = []
    k = 4 ; do { X[--k] = 1 }
    while (k) }
    while (j) ;

    if your '4' is both small and constant, and you don't mind setting to
    "1" rather than 1, this is shorter

    var X, j, A = []
    j = 2 ; do { A[--j] = X = "1111".split("") } while (j) ;

    and if it is variable but under about 54, and ditto,

    var X, j, A = []
    j = 2, k = 99
    do { A[--j] = X = (Math.pow(2, k)-1).toString(2).split("") } while (j) ;

    Inadequately tested.
     
    Dr J R Stockton, Jan 25, 2012
    #8
  9. Archos

    Ross McKay Guest

    Ross McKay, Jan 26, 2012
    #9
  10. In comp.lang.javascript message <3g81i7l6qma04ij6jsef0hjprjochsvq4q@4ax.
    com>, Thu, 26 Jan 2012 11:56:11, Ross McKay <au.org.zeta.at.rosko@invali
    d.invalid> posted:

    I prefer to rely on my own tests, done with a large enough loop for time
    to begin to matter.

    var A, J, K_ = 777 // increase v put code in those
    D0_ = new Date()
    Q_ = K_ ;
    D1_ = new Date()
    Q_ = K_ ; while (Q_--) { }
    D2_ = new Date()
    Q_ = K_ ; while (Q_--) { A = [] ; J = K_ ; while (--J) A[J] = J }
    D3_ = new Date()
    Q_ = K_ ; while (Q_--) { A = [] ; for (J = 0 ; J < K_ ; J++) A[J] = J }
    D4_ = new Date()
    Q_ = [D1_-D0_, D2_-D1_, D3_-D2_, D4_-D3_] // times // Demo 6

    The while loop is clearly shorter. And I find it faster in three
    browsers out of five.
     
    Dr J R Stockton, Jan 27, 2012
    #10


  11. It should be noted that this approach can also be used with multi-digit
    numbers and any other value:

    var a = [];
    a.length = 5;
    a = a.join(",12").split(",").map(function (el) { return +el; });
    a.shift();
    a = [a, a.slice()];

    However more elegant, the map() method is not backwards compatible (so it
    requires comparably expensive emulation) and it is expensive itself because
    the function needs to be called multiple times. Also, the need for
    additional shift() (or pop(), if you use "12," instead) with multi-character
    values reduces its elegance.

    So a combination of the classical looping approach and the more elegant
    slicing approach (which performs the element assignment natively, therefore
    should be cheaper than an explicit loop) appears to be the best solution:

    function getFilledMatrix(rows, columns, fill)
    {
    var a = [];

    var tmp = [];
    tmp.length = columns;

    for (var j = 0; j < columns; ++j)
    {
    tmp[j] = fill || 0;
    }

    for (var i = 0; i < rows; ++i)
    {
    if (i == 0)
    {
    a.push(tmp);
    }
    else
    {
    a.push(tmp.slice());
    }
    }

    return a;
    }


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 28, 2012
    #11
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.