惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
Recent Announcements
Recent Announcements
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More

ByteofDev

Turning Claude into Postgres so I can raise a Series A Making Postgres 42,000x slower because I am unemployed A Quick(ish) Introduction to Tuning Postgres JavaScript numbers have an (adoption) problem My 2025 JavaScript Wishlist JavaScript Benchmarking Is a Mess Replacing Disqus Commenting A deep dive into distributed database architectures 10 ways to speed up web image loading Tailwind has a scalability problem. How can we solve that? Lerna vs Turborepo vs Rush: Which is better in 2023? The 6 JavaScript Projects to watch in 2023 Top 5 Alternatives to React in 2023 How to use ESM on the web and in Node.js React vs Svelte: Which is better in 2023? 10 ways to speed up JavaScript loading What is Bun, and does it live up to the hype? State of JS 2021 Results and Analysis State of the Web: React 10 ways to speed up web font loading State of the Web: Atomic CSS State of the Web: Static Site Generators State of the Web: Bundlers & Build Tools Migrating ByteofDev from SvelteKit to Astro State of the Web: Serverless Functions State of the Web: Deno Array.map() versus Array.forEach() How I Built the Fastest JavaScript Data Differ State of the Web: WebAssembly When you should and shouldn't use React
A quick introduction to JavaScript Maps
Jacob Jackson · 2022-01-10 · via ByteofDev

Map is a JavaScript datatype for storing key/value data while remembering insertion order, which was introduced in ES6. While it is not as well known as Arrays or Objects, it still can be helpful. This article has a short intro to Maps and when to use them.

How to use Maps

Maps are fairly simple to use. However, they use different methods than arrays or objects, like not using dot notation and using classes, so there is some to learn.

Creating a map

To create a Map, you run new Map() and pass an array of key/value pairs.

const map = new Map([["propertyName", "value"]]);

Reading a value

To read a value from a Map, you can use map.get("propertyName").

const map = new Map([["propertyName", "value"]]);
console.log(map.get("propertyName"));
// expected output: "value"

Setting a value

To set a value in a Map after initialization, you can use map.set("propertyName","value").

const map = new Map([["propertyName", "value"]]);
map.set("propertyName", "newValue");
console.log(map.get("propertyName"));
// expected output: "newValue"

Deleting a value

To delete a value, you can use map.delete("propertyName")

const map = new Map([["propertyName", "value"]]);
map.delete("propertyName");
console.log(map.get("propertyName"));
// expected output: undefined

That is it! Now you should know the basics of using Maps. Now we will look at how they differ from Objects and Arrays.

What makes Maps different

Differences from Objects

  • Maps retain insertion order, like Arrays.
  • Maps can have keys of any type, like number or boolean, instead of just strings.
  • JSON does not support Maps. However, some other serialization formats do.
  • Maps are optimized for frequently changing data.

Differences from Arrays

  • Maps use keys like Objects.
  • As said before, JSON does not support Maps. However, some other serialization formats do.
  • Maps are optimized for frequently changing data.

Use cases for maps

  • K/V data that reliably retains insertion order.
  • Frequently changing data.
  • When you need keys of different types

Conclusion

Hopefully, now you know how and when to use Maps. If you want to learn more about things like this, be sure to subscribe to the mailing list. Thanks for reading.