0

Given the following array:

var list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

and these for loops:

for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
       document.getElementById("someID").innerHTML = 
    }
} 

how can i print them in a 'textarea' having the following format:

1 2 3
4 5 6
7 8 9

2
  • Does it have to be for loops? Commented Nov 13, 2014 at 20:39
  • 1
    You should probably be using e.g. lists.length and list[i].length` instead of hard-coding the sizes. Commented Nov 13, 2014 at 20:40

6 Answers 6

1

If it doesn't have to be for loops, you can also use map:

var list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    someId = document.getElementById( 'someID' );

someId.innerHTML = '<pre>' + list.map(function (row) {
    return row.join(' '); 
}).join('<br>') + '</pre>';
<div id="someID"></div>

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

Comments

0

so maybe something like...

var list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

var html = '';
for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      html += list[i][j] + " ";               
    }
    html += '<br />';
} 
  
  document.getElementById("content").innerHTML = html;
<div id="content">
  
</div>

1 Comment

Instead of checking inside the inner loop when to add the<br/>, just do it unconditionally in the outer loop after the inner loop.
0

Construct the string to output in the inner loops, and after the outer loop you make your element to show that string.

1 Comment

Belive me, I have tried such solutions, but didn`t manage to pull this off... can you give me an example maybe? Thanks in advance.
0

Something like this iterating through each idea:

add <div id="someID"></div>

Then instead of hard coding 3 use list.length and list[i].length then do:

var list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

var temp = "";
for (i = 0; i < list.length; i++) {
    for (j = 0; j < list[i].length; j++) {
       temp+=list[i][j] + " ";
    }
    temp+="<br>";
} 
document.getElementById("someID").innerHTML = temp;

Working JSFiddle: http://jsfiddle.net/0k1tf5wh/

Hope this helps!!

Comments

0

To display it in a div, you'll need to wrap each row in a div, because of how HTML handles whitespace:

See: JSFiddle

var list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var html = '';
for (i = 0; i < list.length; i++) {
    html += list[i].join(' ');
}
document.getElementById('someID').innerHTML = html;

Comments

0

There are many ways to do that.

console.log will print one line per call on most browsers.

You can use line breaking characters ('\n') to print to a file, if you are working on an environment where you have access to IO (i.e.: NodeJS).

In HTML you can use the <br> tag, paragraphs (<p>), or you can place each line in a div. For example, with <br> you could do it like this:

var list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

for (i = 0; i < list.length; i++) {
    document.getElementById("someID").innerHTML += list[i].join(" ") + "<br>";
}

Remember that each element of the outer array is an array itself, so you can print them as one element each with the join method from the Array type.

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.