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

推荐订阅源

D
Docker
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
美团技术团队
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
T
Tailwind CSS Blog
U
Unit 42
C
Check Point Blog
S
SegmentFault 最新的问题
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
罗磊的独立博客
小众软件
小众软件
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net

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.