Easiest way to convert JavaScript object to map!

hasnaindev

Muhammad Hasnain

Posted on January 1, 2021

Easiest way to convert JavaScript object to map!
const person = {
  name: 'Hasnain',
  age: 22,
  profession: 'Web Developer',
};

const map = new Map(Object.entries(person));

console.log(map);
Enter fullscreen mode Exit fullscreen mode

When and why to use Map over Object? 🤔

There are a lot of excellent people like you, with tons of knowledge out there writing awesome answers! Here is an amazing stackoverflow post I found that explains just that! 🎉🎊

You are lazy 🥴

I know you are lazy like me (every good developer should be), so I'd like to briefly describe the difference between Object and Map. With that information, be the judge of when to use which! 😎

According to Mozilla:

  1. A Map's key can be any value, including primitive types, objects and functions.
  2. You can use for...of loop to iterate Map in the insertion order, meaning what you first inserted will be the first element to show up in the loop and so on.
  3. Just as Array have length property, Map comes with a size property.
  4. Performs better when a lot of insertion and removal operations are involved.
💖 💪 🙅 🚩
hasnaindev
Muhammad Hasnain

Posted on January 1, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related