ES6 - Map Method set()



This function adds key and value to map.

Syntax

The syntax for set() is given below where, key is the key of the element to add to the Map object and value is the value of the element to add to the Map object.

map_name.set(key, value);

Example

<script>
   let andy = {ename:"Andrel"},
      varun = {ename:"Varun"},
      prijin = {ename:"Prijin"}
   
   let empJobs = new Map();
   empJobs.set(andy,'Programmer')
   empJobs.set(varun,'Accountant')
   empJobs.set(prijin,'HR')

   console.log(empJobs)
</script>

The output of the above code is as mentioned below −

{{…} => "Programmer", {…} => "Accountant", {…} => "HR"}
Advertisements