How to use Map object and spread operator In Javascript
This method demonstrate how we can extend a given map or insert the pairs from a new map using the spread operator
Example: In this example, we will extend the map1 using spreate operator to iterate the map2 key-value pairs.
const map1 = new Map([
[1, 10],
[2, 21],
]);
const map2 = new Map([
["a", 100],
["b", 200],
]);
map1.set(...map2);
console.log(map1);
Output
Map(3) { 1 => 10, 2 => 21, [ 'a', 100 ] => [ 'b', 200 ] }
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()
Contact Us