How to use Array map() Method In Javascript
In this method we will create a JavaScript map object from a 2 dimensional array without using the map constructor by using array.map() method.
Example: In this example, we will create an array map from given 2-d array.
// Given array
const arr = [
["a", 100],
["b", 200],
];
// Map object
let map1 = new Map();
// Iterate the array and insert pairs to map
arr.map(([key, value]) => map1.set(key, value));
// Display output
console.log(map1);
Output
Map(2) { '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