Array From Method

Video

JavaScript Notes

JavaScript
    /***************************************
    Array.from()
    -create a new shallow copy array
    ***************************************/
    
    let names = ['Bart','Maggie','Homer','Marge','Lisa'];
    let stuff = [123, 'Apu', 
                 {Smithers: 'Mr. Burns'}, 
                 ['Futurama', 'Disenchantment']];
    
    let nm2 = Array.from(names);
    let st2 = Array.from(stuff);
    
    nm2[4] = 'Flanders';                // does not affect 'names' as the array 'nm2' was copied by value 
                                        // and not by reference, as the array 'names' only has PRIMITIVE values
    
    st2[2].Smithers = 'Not Mr. Burns';  // changes both 'stuff' and 'st2', 
                                        // as the object within 'stuff' was just shallow copied (i.e. referenced)
    
    console.log( stuff);
    console.log( st2);
    
    console.log( names );
    console.log( nm2 );