Last updated: Mar 3, 2024
Reading timeยท3 min
Pass an array containing arrays of key-value pairs to the Map()
constructor
to initialize a Map
with values.
The first element in each array should be the key and the second, the value.
// โ Create a Map from an Array const map1 = new Map([ ['name', 'bobby hadz'], ['country', 'Chile'], ]); // ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'country' => 'Chile' } console.log(map1); console.log(map1.get('name')); // ๐๏ธ bobby hadz
We passed a two-dimensional array to the
Map()
constructor to initialize the Map
with values.
The first element in each nested array is the key and the second is the value.
You can also convert a Map
to a two-dimensional array.
const arr = Array.from(map1); // ๐๏ธ [['country], 'Chile], ['name', 'Tom'], ['age', 30]] console.log(arr);
We used the Array.from() method to
convert a Map
object to an array.
Alternatively, you can use the Object.entries()
method.
The Object.entries()
method returns an array of key-value pairs that can be
used to initialize a Map
.
const obj = {name: 'bobby hadz', country: 'Chile'}; const map1 = new Map(Object.entries(obj)); // ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'country' => 'Chile' } console.log(map1);
The Object.entries() method returns a two-dimensional array, where each nested array contains a key and a value.
const obj = {name: 'bobby hadz', country: 'Chile'}; // ๐๏ธ [ [ 'name', 'bobby hadz' ], [ 'country', 'Chile' ] ] console.log(Object.entries(obj));
The Map
constructor expects an array of key-value pairs as a parameter, so the
Object.entries()
method does exactly what we need.
An alternative approach is to create an empty Map
and manually set its
key-value pairs.
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('country', 'Chile'); map1.set('age', 30); // Map(3) { 'name' => 'bobby hadz', 'country' => 'Chile', 'age' => 30 } console.log(map1); console.log(map1.get('name')); // ๐๏ธ bobby hadz
We used the
Map()
constructor to create an empty Map
object.
The Map.set() method takes a key
and a value as parameters and adds the key-value pair to the Map
object.
The Map.set
method returns the Map
object, so you can chain calls to the
method.
const map1 = new Map(); map1 .set('name', 'bobby hadz') .set('country', 'Chile') .set('age', 30) .set('salary', 50); // Map(4) { // 'name' => 'bobby hadz', // 'country' => 'Chile', // 'age' => 30, // 'salary' => 50 // } console.log(map1);
On each call to the Map.set()
method, the Map
object gets returned, so we
can call the set()
method again.
If you set the value of the same key multiple times, the last value wins.
const map1 = new Map(); map1.set('name', 'alice'); map1.set('name', 'bobby'); console.log(map1); // ๐๏ธ Map(1) { 'name' => 'bobby' } console.log(map1.get('name')); // ๐๏ธ bobby
The Map.get() method takes a key as a parameter and returns the corresponding value.
You can also initialize a Map with values from another Map.
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('country', 'Chile'); map1.set('age', 30); const map2 = new Map(map1); // ๐๏ธ Map(3) { 'name' => 'bobby hadz', 'country' => 'Chile', 'age' => 30 } console.log(map2); console.log(map2.get('name')); // ๐๏ธ bobby hadz console.log(map2.get('country')); // ๐๏ธ Chile
We created a Map
object, added 3 key-value pairs to it and used it to
initialize a new Map
object.
You can directly pass one Map
object to the Map()
constructor to create
another Map
object.
You can learn more about the related topics by checking out the following tutorials: