float
float

Reputation: 11

Javascript array becomes null

I defined two-dimensional array, tried to fill it in nested loop, but it fill only first dimension with right values, other dimensions are filled with null(or undefined), thanks.

var Arr = [];
var i =0;

for(i=0;i<13;i++)
{
  Arr[i]=[];
}

var a=0,b=0;

for(a=0;a<13;a++)
{
  for(b=0;b<13;b++)
  {
    Arr[[a][b]]=AnotherArrWithRightValues[(13 * a) + b];
  }
}

Upvotes: 0

Views: 212

Answers (4)

Sainath
Sainath

Reputation: 21

Try this,

var arr =[];

for(var a=0;a<13;a++)
  {
    arr[a] = new Array();
    for(var b=0;b<13;b++)
      {
        arr[a].push((13 * a) + b);
      }
  }

i hope this will help you

Upvotes: 0

Karthik VU
Karthik VU

Reputation: 561

Loksly's answer is correct, but implemented in a different way. To answer your question, replace Arr[[a][b]] with Arr[a][b].


Full Code :

var Arr = [];

for(var a = 0 ; a < 13 ; a++) {
  Arr[a] = [];
  for(var b = 0 ; b < 13 ; b++) {
    Arr[a][b]=AnotherArrWithRightValues[(13 * a) + b];
  }
}

Upvotes: 1

Loksly
Loksly

Reputation: 124

Just for the record, another way to achieve the same:

var Arr = [];
var i = 0, tmp;
var a, b;

for(a = 0; a < 13; a++){
    tmp = [];
    for(b = 0; b < 13; b++){
        tmp.push(AnotherArrWithRightValues[i++]);
    }
    Arr.push(tmp);
}

Upvotes: 0

challenger
challenger

Reputation: 2214

Arr[[a][b]] should be Arr[a][b]

Upvotes: 1

Related Questions