M2D4 – ITP1 2D Arrays

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

console.log(my2DArray[0]); //prints 1, 2, 3

console.log(my2DArray[0][0]; //prints 1
console.log(my2DArray[1][2]; //prints 6
console.log(my2DArray[2][3]; //prints 10
console.table(groups); //prints a table of the array groups in the console
var groups =[["Angus", "Ashley", "Anthony", "Anna", "Alicia"],
            ["Benny", "Beau", "Bernie", "Betty", "Bjorn"],
            ["Colin", "Chris", "Claire", "Catherine", "Cody"],
            ["Daisy", "Delia", "Derek", "Donald", "Danny"],
            ["Ernest", "Elanor", "Eliza", "Eric", "Elizabeth"],
            ["Frida", "Freddy", "Fiona", "Finn", "Fatima"]];

function setup()
{
  for(var i = 0; i < groups.length; i ++)
    {
        console.log(groups[i]); //prints all the 1st dimension array of groups
    }
}
var groups =[["Angus", "Ashley", "Anthony", "Anna", "Alicia"],
            ["Benny", "Beau", "Bernie", "Betty", "Bjorn"],
            ["Colin", "Chris", "Claire", "Catherine", "Cody"],
            ["Daisy", "Delia", "Derek", "Donald", "Danny"],
            ["Ernest", "Elanor", "Eliza", "Eric", "Elizabeth"],
            ["Frida", "Freddy", "Fiona", "Finn", "Fatima"]];

function setup()
{
  for(var i = 0; i < groups.length; i ++)
    {
//        console.log(groups[i]);
        for(var j = 0; j < groups[i].length; j ++) //we are iterating for the number of objects in each array
        {
            console.log(groups[i][j]);
        }
    }
}

Leave a Reply