modern-array-methods
它能做什么
这项技能检查 JavaScript 代码,并在循环掩盖了意图的地方建议使用现代数组和对象方法。适用于希望代码更简洁、更易理解的开发者。
适用场景
- ✓当你请智能体清理或现代化 JavaScript 循环的时候。
- ✓当某个函数中有很长的 for 或 while 循环,用来过滤、转换或查找数据的时候。
- ✓当你审查代码,并希望找到更安全的对象复制或数据转换方式的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/modern-array-methods/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我用现代数组方法重写 JavaScript 文件中的循环,让它更易读。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出可以用 map、filter、reduce、find 或 flatMap 替代的循环,并加以改写,使每一步的用途可以从方法名中看出来。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Use modern array and object methods
Modern array and object methods produce code that directly expresses intent — a filter() call self-documents that you're selecting items. Manual for loops obscure the purpose behind implementation details. These methods are also well-optimized by JavaScript engines and support chaining for concise data transformations.
Quick Reference
- Use map() to transform arrays, filter() to select items, reduce() for aggregation
- Use find() and findIndex() instead of manual loops with break
- Use Object.entries() and Object.fromEntries() to transform objects
- Use structuredClone() for deep copying instead of JSON.parse(JSON.stringify())
Check
Find loops in this file that could be replaced with modern array methods like map, filter, reduce, find, or flatMap.
Fix
Replace manual for/while loops with appropriate array methods to make the code more declarative.
Explain
Explain map, filter, reduce, find, flatMap, and Object.entries with practical examples.
Code Review
Review scripts, client components, and browser execution paths related to Use modern array and object methods. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/javascript/modern-array-methods
常见问题
为什么用 filter 而不是循环?
filter 调用一眼就能看出是在筛选项目,而手动循环会把这一点隐藏在细节中。
structuredClone 是做什么的?
这项技能建议用它进行深拷贝,取代较旧的 JSON.parse 和 JSON.stringify 方法。
这会改变代码的行为吗?
结果应当保持不变,但这项技能指出,应在浏览器中验证改动。
详情
作者的原始描述
Use when reviewing scripts, client components, bundles, or runtime behavior related to Use modern array and object methods. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.
热度
- GitHub 星标
- 75k
- 排名
- #2943
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。