Last updated: Mar 3, 2024
Reading timeยท3 min
Use the set()
method to add a key-value pair to a Map
, e.g.
map.set('myKey', 'myValue')
.
The set()
method adds or updates the element with the provided key and value
and returns the Map
object.
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('age', 30); // ๐๏ธ๏ธ Map(2) { 'name' => 'bobby hadz', 'age' => 30 } console.log(map1); console.log(map1.get('name')); // ๐๏ธ bobby hadz
We used the
Map.set()
method to add key-value pairs to a Map
object.
The method takes 2 parameters:
If the key already exists in the Map
object, its value is updated.
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('age', 30); console.log(map1.get('name')); // ๐๏ธ bobby hadz map1.set('name', 'alice'); console.log(map1.get('name')); // ๐๏ธ alice
We called the set()
method with an existing key (name
), so the value of the
key got updated.
You can also pass an array of key-value pairs to the Map
constructor to
initialize the Map
with values.
const map1 = new Map([ ['name', 'bobby hadz'], ['age', 30], ['country', 'Chile'], ['city', 'Santiago'], ]); // Map(4) { // 'name' => 'bobby hadz', // 'age' => 30, // 'country' => 'Chile', // 'city' => 'Santiago' // } console.log(map1);
The Map()
constructor can be called with an array of key-value pairs.
If you need to add the key-value pairs of an object to a Map, use the
Object.entries()
method.
const obj = { name: 'bobby hadz', age: 30, }; const map1 = new Map(Object.entries(obj)); // ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'age' => 30 } console.log(map1);
The Object.entries()
method returns an array of key-value pairs.
const obj = { name: 'bobby hadz', age: 30, }; // ๐๏ธ [ [ 'name', 'bobby hadz' ], [ 'age', 30 ] ] console.log(Object.entries(obj));
Map.set()
methodThe Map.set()
method returns the Map
object. This allows us to chain
multiple calls to the set()
method.
const map1 = new Map(); map1 .set('name', 'bobby hadz') .set('number', 5) .set('color', 'blue') .set('city', 'Santiago'); // Map(4) { // 'name' => 'bobby hadz', // 'number' => 5, // 'color' => 'blue', // 'city' => 'Santiago' // } console.log(map1);
On each call to the set()
method, the Map
object is returned, so we can
chain multiple calls to the method.
You can add key-value pairs to a Map
object conditionally by using the
Map.has()
method.
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('age', 30); if (!map1.has('name')) { map1.set('name', 'alice'); } // ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'age' => 30 } console.log(map1); // ๐๏ธ bobby hadz console.log(map1.get('name'));
We used the Map.has()
method to check if the name
key is contained in the
Map
object before setting a value for the key.
The Map.has() method returns
true
if the specified key is contained in the Map
and false
otherwise.
Note that the keys and values of a Map
can be of any type, e.g. an object.
const map1 = new Map(); const obj = {country: 'Chile'}; map1.set(obj, {city: 'Santiago'}); // ๐๏ธ { {country: 'Chile'} => {city: 'Santiago'} } console.log(map1); console.log(map1.get(obj)); // ๐๏ธ {city: 'Santiago'}
The code sample uses an object as a key in the Map
.
I've also written an article on how to add a key-value pair to an object.
You can learn more about the related topics by checking out the following tutorials: