0

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];
  }
}
5
  • 3
    Did you try Arr[a][b]=AnotherArrWithRightValues[(13 * a) + b]; Commented Jan 31, 2017 at 13:32
  • 4
    isn't Arr[a][b]? Commented Jan 31, 2017 at 13:32
  • 3
    you have a syntax error: Arr[a][b] =AnotherArrWithRightValues[(13 * a) + b]; Commented Jan 31, 2017 at 13:33
  • where is AnotherArrWithRightValues defined ? Commented Jan 31, 2017 at 13:34
  • AnotherAWRV is global array Commented Jan 31, 2017 at 13:38

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

@Mohsen How so?
@Mohsen_Fatemi really! how's that? this Arr[[a][b]] is syntax error !
@Mohsen_Fatemi That's what is expected
1

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];
  }
}

2 Comments

Your answer is exactly like mine
Simultaneously written !
0

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);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.