I'm trying to print an array using for in javascript, but it does not display correctly

1

Print manually and then tried to use for to print, but it turned out I could not see it properly.

let mochila = new Array();
let item1 = ['corda', 2],
    item2 = ['faca', 3],
    item3 = ['cura', 23],
    item4 = ['prego', 35];

mochila.push(item1);
mochila.push(item2);
mochila.push(item3);
mochila.push(item4);

document.write(mochila[0][0] + ' - ' + mochila[0][1] + '<br/>');
document.write(mochila[1][0] + ' - ' + mochila[1][1] + '<br/>');
document.write(mochila[2][0] + ' - ' + mochila[2][1] + '<br/>');
document.write(mochila[3][0] + ' - ' + mochila[3][1] + '<br/>');

document.write('----------------------------------------<br/>');

for(let i = 0; i < item1.length; i++){
    for(let j = 0; j < item1.length; j++){
        document.write(mochila[i][j] + ' - ' + mochila[i][j] + '<br/>');
    }
}
    
asked by anonymous 07.11.2018 / 13:30

2 answers

0

See if that's it:

let mochila = new Array();
let item1 = ['corda', 2],
    item2 = ['faca', 3],
    item3 = ['cura', 23],
    item4 = ['prego', 35];

mochila.push(item1);
mochila.push(item2);
mochila.push(item3);
mochila.push(item4);

document.write(mochila[0][0] + ' - ' + mochila[0][1] + '<br/>');
document.write(mochila[1][0] + ' - ' + mochila[1][1] + '<br/>');
document.write(mochila[2][0] + ' - ' + mochila[2][1] + '<br/>');
document.write(mochila[3][0] + ' - ' + mochila[3][1] + '<br/>');

document.write('----------------------------------------<br/>');

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

  document.write ('<br>');
}

or fixed sizes:

let mochila = new Array();
let item1 = ['corda', 2],
    item2 = ['faca', 3],
    item3 = ['cura', 23],
    item4 = ['prego', 35];

mochila.push(item1);
mochila.push(item2);
mochila.push(item3);
mochila.push(item4);

document.write(mochila[0][0] + ' - ' + mochila[0][1] + '<br/>');
document.write(mochila[1][0] + ' - ' + mochila[1][1] + '<br/>');
document.write(mochila[2][0] + ' - ' + mochila[2][1] + '<br/>');
document.write(mochila[3][0] + ' - ' + mochila[3][1] + '<br/>');

document.write('----------------------------------------<br/>');

for(let i = 0; i < mochila.length; i++)
{
    document.write(mochila[i][0] + ' - ' + mochila[i][1] + '<br/>');
}
    
07.11.2018 / 14:04
-1

let mochila = new Array();
let item1 = ['corda', 2],
    item2 = ['faca', 3],
    item3 = ['cura', 23],
    item4 = ['prego', 35];

mochila.push(item1);
mochila.push(item2);
mochila.push(item3);
mochila.push(item4);

document.write(mochila[0][0] + ' - ' + mochila[0][1] + '<br/>');
document.write(mochila[1][0] + ' - ' + mochila[1][1] + '<br/>');
document.write(mochila[2][0] + ' - ' + mochila[2][1] + '<br/>');
document.write(mochila[3][0] + ' - ' + mochila[3][1] + '<br/>');

document.write('----------------------------------------<br/>');

for(let i = 0; i < mochila.length; i++){
    document.write(mochila[i][0] + ' - ' + mochila[i][1] + '<br/>');

}
    
07.11.2018 / 14:20