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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
量子位
A
Arctic Wolf
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
博客园 - Franky
C
Cyber Attacks, Cyber Crime and Cyber Security
The Cloudflare Blog
Last Week in AI
Last Week in AI
The Hacker News
The Hacker News
I
Intezer
J
Java Code Geeks
P
Privacy International News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
S
Securelist
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Jina AI
Jina AI
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
雷峰网
雷峰网
T
Tenable Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 【当耐特】
T
Threat Research - Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
N
News | PayPal Newsroom
Google Online Security Blog
Google Online Security Blog
K
Kaspersky official blog
H
Help Net Security
宝玉的分享
宝玉的分享
罗磊的独立博客
Webroot Blog
Webroot Blog
月光博客
月光博客
B
Blog RSS Feed
Recorded Future
Recorded Future

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 A quick introduction to JavaScript Maps How I Built the Fastest JavaScript Data Differ State of the Web: WebAssembly When you should and shouldn't use React
Array.map() versus Array.forEach()
Jacob Jackson · 2022-01-10 · via ByteofDev

If you are learning about iterating through arrays, you have likely come across two different methods. Array.map() and Array.forEach(). At first glance, it might seem like they are the same, as they both iterate through arrays and call a function for each item. However, they are different, and many people mix them up, which can be very bad.

What Array.forEach() does

Array.forEach() is pretty simple. When you run .forEach(function) on an array, it calls function for each element with that element passed to it.

const fruits = ["bananas", "pears", "oranges"];

fruits.forEach((fruit) => {
	console.log(fruit);
});
// expected output: "bananas"
// expected output: "pears"
// expected output: "oranges"

Array.forEach() does not return anything, so if you assigned a variable to it, the variable would be set to undefined.

What Array.map() does differently

Array.map() is similar in most ways. However, it differs in one important way. Instead of not returning anything, it returns an array of new values from the result of the function callback.

let fruits = ["bananas", "pears", "oranges"];

fruits = fruits.map((fruit) => {
	return `${fruit} are awesome`;
});

console.log(fruits);
// expected output: ["bananas are awesome", "pears are awesome", "oranges are awesome"]

This can make it easy to modify every value in an array. However, it is commonly misused to do what Array.forEach() is intended to do.

Conclusion

Array.forEach() and Array.map() are related, but not the same. Use Array.map() when trying to modify every item in an array, and Array.forEach() otherwise, because you are less likely to mess up your code, and it is usually faster. Enforcing these rules also makes your code clearer and improves quality. If you found this interesting, be sure to sign up for the mailing list below to get tips and posts like these delivered to your mailbox. I hope you learned something from this, and thank you for reading.