Chained Shift Method ( ) and Shallow Copy

Video

JavaScript Notes

JavaScript
    /**************************************
    Chained Shift() method and Shallow Copy
    **************************************/
    
    let a = [[1], [2], [3]];       // 2-D array
    let b = [...a];                // Uses the Spread Operator - it creates a shallow copy of the arrays
    console.log('1a',a);
    console.log('1b',b);
    
    let c = b.shift().shift();     // The double shift just returns 1, (first array element of first array element of b)
    console.log(c);

    console.log('1a',a);           // first array in 'a' is empty, but still exists
    console.log('1b',b);           // first array in 'b' is removed
    
    let d = b.shift();
    console.log('2a',a);           // 'a' has all three arrays      
    console.log('2b',b);           // 'b' has first array element removed     
    
    //
    let e = d.shift();
    console.log('3a',a);           // first array in 'a' is empty, but still exists
    console.log('3b',b);           // 'b' has first array element removed
    
    //
    console.log(d);                // shows an empty array
    console.log(e);                // shows the number 1
    
    d.push(123);
    console.log(d);                // changes 'd'
    console.log(a);                // but also changes first array in 'a'