Nested Loops with Arrays & Objects

Video

JavaScript Notes

JavaScript
    // nested loops and multi-dimensional objects
    // we can used nested loops to access all the elements
    // inside multi-dimensional arrays or objects
    
    let twoD = [[1, 2, 3, 4, 5, 6, 7],
                [8, 10, 5, 7, 3, 22, 6, 42],
                [123, 54, 12, 11, 9, 15]]
    
    letbigHero = {characters :[
        {name: 'Hiro', voice: 'Ryan Potter'},
        {name: 'Baymax', voice: 'Scott Adsit'},
        {name: 'Go Go Tamago', voice: 'Jamie Chung'},
        {name: 'Fred' voice: 'T.J. Miller'}
    ]};
     
    // nested for loops
    let rows = twoD.length; for(let i=0; i<rows; i++){
        let items = twoD[i].length; 
        for (let n=0; n<items; n++){ 
            console.log(twoD[i][n]);
        }
    }
    
    let chars = bigHero['characters'] // bigHero.characters 
    for (let i = 0, len = chars.length; i < len; i++){
        console.log(chars[i].name); 
        console.log(chars[i]['voice']); 
        for (let prop in chars[i]){
            console.log(prop, chars[i][prop]); // cannot use the .dot notation
        }
    }