Maps & Sets

Video

JavaScript Notes

JavaScript
    /********************************************************* 
    Maps – Like Objects they have keys and values
    (unique keys) – same as Objects
    .size
    .set(key, value)
    .delete(key)
    .get(key)
    .has(key)
    .keys( ) – returns Iterator with array of keys
    .values( ) – returns Iterator with array of values
    .entries( ) – returns Iterator with array of [key, values]
    .clear( )
    .forEach( )
    Sets – Like Arrays they have values 
    (unique values) - remove non-unique values from an Array
    .size
    .add(value)
    .delete(value)
    .entries( ) – returns Iterator with array of [value, value]
    .values( ) – returns Iterator with array of values
    .has(value)
    .clear( )
    .forEach( )
    ********************************************************/ 
    
    let log = console.log;
    let obj = {
        prop1: 123,
        prop2: 'abc',
    }
    obj.prop2 = 'def'; // keys need to be unique, so this will overwrite the value of the existing key
    
    // let map = new Map(obj); //fails – Objects are not iterable 
    let map = new Map( );
    map.set('prop1', 123);
    map.set('prop2', 'abc');
    
    let names = ['Rob', 'Bran', 'Arya', 'Sansa', 'Rickard']; 
    names.push('Rob');
    let set = new Set(names);	// will automatically remove the duplicate value, 'Rob'