How to use map set() Method In Javascript
In this method, we use the JavaScript map.set() method to insert the pair into the map.
Syntax:
myMap.set(key,value);
Example: In this example, we will insert some key-value pairs using map.set method.
const map1 = new Map();
// Insert pairs
map1.set('First Name','Jatin');
map1.set('Last Name','Sharma');
map1.set('website','w3wiki')
// Display output
console.log(map1)
Output
Map(3) { 'First Name' => 'Jatin', 'Last Name' => 'Sharma', 'website' => 'w3wiki' }
How to Add a key/value Pair to Map in JavaScript ?
This article will demonstrate how we can add a key-value pair in the JavaScript map. JavaScript Map is a collection of key-value pairs in the same sequence they are inserted. These key values can be of primitive type or the JavaScript object.
All methods to add key-value pairs to the JavaScript Map:
Table of Content
- Map constructor
- Using map set() Method
- Using Map object and spread operator
- Using Array map() Method
- Using Object.entries() and Spread Operator
- Using Array.prototype.forEach()