10

I have the following array (the code is written in Java) :

String[][] a = new String[3][2];
a[0][0] = "1"; 
a[0][1] = "2"; 

a[1][0] = "1";  
a[1][1] = "2"; 

a[2][0] = "1";
a[2][1] = "2"; 

and what I want to do is to print 111222 and I accomplished that in Java by doing this:

for (int i=0;i < a[i].length;i++){
    for(int j=0;j <a.length;j++){
        System.out.print(a[j][i]);
    }
}

What is the equivalent of this in JavaScript?

0

6 Answers 6

10

Here is the equivalent code in Javascript (no space its not a script version of java)

! edit missed the particulars of the loops, fixed now

var a = [];
a.push(["1", "2"]);
a.push(["1", "2"]);
a.push(["1", "2"]);

for(var i = 0; i < a[i].length; i++) {
  for(var z = 0; z < a.length; z++) {
    console.log(a[z][i]);
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

this is not right, it will print the array like this (121212) what i want is to print something like the following 111222
the for loop bit is the same as java write it as you whant
i didn't se that you had nested it strangely is that really the behavior you want?
Yes, i want to use it for a special purpose function.
wow, can some one elaborate on this code. Beautiful as it is, I dont understand how a[z][i] is printing what its printing. Thanks
|
6
var arr =[
        [1,2,3],
        [4,5,6],
        [7,8,9]
        ],arrText='';

        for (var i = 0; i < arr.length; i++) {
            for (var j = 0; j < arr[i].length; j++) {
                arrText+=arr[i][j]+' ';
            }
            console.log(arrText);
            arrText='';
        }

Output: enter image description here

Comments

5

js is very powerfull ; just use spread operator

mat = [[1,2,3],[3,4,5]]

mat.forEach(v=>console.log(...v));

// output run the code see the output.
1 2 3
3 4 5

Comments

3
for (i=0; i < a.length; i++) {
   for (j = 0; j < a[i].length; j++) { document.write(a[i][j]); }
}

Though it would be smarter to add all the strings together and the print them out as one (could add to an element or alert it out.)

1 Comment

again this will print the array like this (121212) what i want is to print something like the following 111222
2
  • In javascript you can create multi dimensional array using single dimensional arrays.
  • For every element in an array assign another array to make it multi dimensional.

    // first array equivalent to rows 
    let a = new Array(3);
    
    // inner array equivalent to columns
    for(i=0; i<a.length; i++) {
      a[i] = new Array(2);
    }
    
    // now assign values
    a[0][0] = "1"; 
    a[0][1] = "2"; 

    a[1][0] = "1";  
    a[1][1] = "2"; 

    a[2][0] = "1";
    a[2][1] = "2";
    
    /* console.log appends new line at end. So concatenate before     printing */
    
    let out="";
    for(let i=0; i<a.length; i++) {
      for(let j=0; j<a[i].length; j++) {
        out = out + a[i][j];
      }
    }
    console.log(out);

Comments

1
var a = [];

a[0] = [];
a[0][0] = "1";
a[0][1] = "2";

a[1] = [];
a[1][0] = "1";
a[1][1] = "2";

a[2] = [];
a[2][0] = "1";
a[2][1] = "2";

for (i = 0; i < a[i].length; i++) {
    for (j = 0; j < a.length; j++) {
        document.write(a[j][i]);
    }
}

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.