map

Estimated reading time: 3 minutes

Map is collection of data as key value pairs.

~ map methods

  • size (properies)
  • clear
  • delete
  • entries
  • forEach
  • get
  • set
  • has
  • keys
  • values

set

    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');
    console.log(users);
    console.log(users.get(54))

get

    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');
    console.log(users.get('name'));
    // kamal
let users = new Map([
  ['name','kamal'],
  ['age',21],
  ['phone',{
    home: 999,
    office: 121
  }]
])
console.log(users.get('phone').office);
// 121

has

    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');
    console.log(users.has('name'));
    // true

size

    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');
    console.log(users.size);
    // 3

clear

  <script type="text/javascript">
    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');
    let result = users.clear();
    console.log(users);
 //

delete

  <script type="text/javascript">
    // let users = new Map()
    // users.set('name','kamal');
    // users.set('age',21);
    // users.set('status','dev');
    // let result = users.delete('name');
    // console.log(users);
  </script>

keys

    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');
    console.log(users.keys());
    // {"name", "age", "status"}

values

     let users = new Map()
     users.set('name','kamal');
     users.set('age',21);
     users.set('status','dev');
     console.log(users.values());
    // {"kamal", 21, "dev"}

entries

     let users = new Map()
     users.set('name','kamal');
     users.set('age',21);
     users.set('status','dev');
     console.log(users.entries()); -->
    // {"name" => "kamal", "age" => 21, "status" => "dev"}

forEach

    let users = new Map()
    users.set('name','kamal');
    users.set('age',21);
    users.set('status','dev');

    users.forEach(t => {
      console.log(t);
    })
    // kamal
    // 21
    // dev

soureces

es, es6, js