Skip to main content

Map Method in the Javascript

Published 1 min readMarkdown

What is Map?

Map is the object in the JavaScript that holds the Key-value pairs and it can remember the original insertion order of the keys. In the Map, the value can hold both key and value.

Example :

const myMap = new Map();
myMap.set(0, "zero");
myMap.set(1, "one");
console.log(myMap.get('0'))
// Output : zero

There are some predefined methods in the maps are :

  1. .clear()

  2. .get()

  3. .set()

  4. .has()

  5. .delete()

  6. .forEach()

  7. .size()

  8. .values()

  9. .keys()

  10. .entries()

Iteration in Map

  1. Using for..of loop

const map1 = new Map();
map1.set(0, "zero");
map1.set(1, "one");

for (const [key, value] of map1) {
  console.log(`${key} = ${value}`);
}
// 0 = zero
// 1 = one
  1. Using forEach loop

     const map1 = new Map();
     map1.set(0, "zero");
     map1.set(1, "one");
     map1.forEach((value, key) => {
       console.log(`${key} = ${value}`);
     });
     // 0 = zero
     // 1 = one
    

About the author

  • 😇Don’t Be The Same, Be Better 👨🏻‍💻 #Python Programmer... 👨🏻‍💻 Full Stack Web Developer

No comments yet. Be the first to comment.