I am trying to print an array using for in javascript, but it is not displayed correctly

Asked

Viewed 82 times

1

I printed it manually and then tried using a for to carry out the printing, but I happen not to be able to display 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/>');
    }
}

  • 1

    Shouldn’t be i < mochila.length?

  • It is also interesting to note that the push supports multiple elements. So could do mochila.push(item1, item2, item3, item4);

  • Even the push supporting multiple elements is interesting to note that the entire matrix could be created using array literals

  • I gave it a neat one in the code, but I don’t know how to use it. I put itens1.length for me to carry out the 4 impressions, so at the time I use and j to print the matrix it ends up leaving in a wrong way.

  • Already followed @bfavaretto’s tip ? Your first limit for is wrong and should be adjusted to what has been indicated

  • I’ve tried, she actually duplicates the print, displaying more than she should, just the wrong way.

  • Got it, thanks for your help, guys.

Show 2 more comments

1 answer

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/>');
}
  • Entedi, vlw for help. Thank you!

  • You can ask me just one question, I was a little misunderstood [i] that was put in the second for backpack[i].length. for(Let j = 0; j < backpack[i]. length; j++)

  • in case vc is referencing the number of columns?

  • Voce says in the example of fixed sizes?

  • No, the first one. But I was able to understand, I left a bit of the pc and went to the role, so I could understand rsrsrsrs. Thanks for your attention!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.