Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Map Methods in JavaScript

Js Map Methods in JavaScript

?? JavaScript Map Methods

A Map is a collection of key-value pairs where keys can be any data type (objects, functions, primitives). It remembers the insertion order of keys.


? Creating a Map

const myMap = new Map();

? Common Map Methods

MethodDescriptionExample
set(key, value)Adds or updates a key-value pairmyMap.set('name', 'Alice');
get(key)Retrieves value by keymyMap.get('name'); // 'Alice'
has(key)Checks if a key existsmyMap.has('name'); // true
delete(key)Removes a key-value pairmyMap.delete('name');
clear()Removes all entriesmyMap.clear();
size (property)Returns number of entriesmyMap.size;
keys()Returns an iterator of all keysmyMap.keys();
values()Returns an iterator of all valuesmyMap.values();
entries()Returns an iterator of [key, value] pairsmyMap.entries();
forEach(callback)Calls callback for each [key, value] pairmyMap.forEach((val, key) => console.log(key, val));

? Example Usage

const map = new Map();// Add entriesmap.set('name', 'John');map.set(1, 'one');map.set(true, 'yes');// Get valueconsole.log(map.get('name'));  // John// Check existenceconsole.log(map.has(1));  // true// Size of mapconsole.log(map.size);  // 3// Iterate over map entriesfor (const [key, value] of map.entries()) {  console.log(`${key} => ${value}`);}

? Output

Johntrue3name => John1 => onetrue => yes

Would you like examples on how to convert Maps to arrays or JSON?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql