Horje
What is Map Coercion in JavaScript ?

In JavaScript, Map coercion refers to the automatic conversion of an object or an iterable (such as an array) into a Map using the Map constructor. This coercion happens implicitly when an object or iterable with key-value pairs is provided as an argument to the Map constructor.

Example: Here, the array keyValuePairs contains nested arrays representing key-value pairs. When this array is passed as an argument to the Map constructor, it is coerced into a Map, and the resulting Map (myMap) contains the key-value pairs from the array.

Javascript

const keyValuePairs = [
  ['key1', 'value1'],
  ['key2', 'value2'],
  ['key3', 'value3']
];
 
// Map coercion using the Map constructor
const myMap = new Map(keyValuePairs);
 
console.log(myMap);

Output

Map(3) { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Can a Map use Functions as Keys? Can a Map use Functions as Keys?
What is Destructuring in ES6 ? What is Destructuring in ES6 ?
What is the DOM (Document Object Model) ? What is the DOM (Document Object Model) ?
What is LocalStorage & SessionStorage Objects in JavaScript ? What is LocalStorage & SessionStorage Objects in JavaScript ?
How to use the Arguments Object in a Function in JavaScript ? How to use the Arguments Object in a Function in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11