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

推荐订阅源

N
Netflix TechBlog - Medium
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LangChain Blog
G
Google Developers Blog
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
博客园_首页
S
SegmentFault 最新的问题
The Cloudflare Blog
NISL@THU
NISL@THU
T
The Exploit Database - CXSecurity.com
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
H
Help Net Security
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
U
Unit 42
S
Schneier on Security
博客园 - 司徒正美
Help Net Security
Help Net Security
O
OpenAI News
PCI Perspectives
PCI Perspectives
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
Forbes - Security
Forbes - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threatpost
V
Vulnerabilities – Threatpost
美团技术团队
P
Palo Alto Networks Blog
TaoSecurity Blog
TaoSecurity Blog
V2EX - 技术
V2EX - 技术
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ

ByteofDev

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.