Easiest way to convert JavaScript object to map!
Muhammad Hasnain
Posted on January 1, 2021
const person = {
name: 'Hasnain',
age: 22,
profession: 'Web Developer',
};
const map = new Map(Object.entries(person));
console.log(map);
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:
- A Map's key can be any value, including primitive types, objects and functions.
- 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.- Just as Array have
length
property, Map comes with asize
property.- Performs better when a lot of insertion and removal operations are involved.
💖 💪 🙅 🚩
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
javascript How to get the common values of an indefinite number of arrays in Javascript
December 24, 2020