ES6: These 8 hidden cheats, knowing 3 counts as my loss!

Tags: ES6, JavaScript, Performance Optimization, Code Simplification

Writing less code naturally leads to fewer bugs. ”Lu Xun (not mentioned)

Today, let's not talk about React or Vue. Let's go back to the language layer and dig up those ES6 obscure APIs that are "given by the official, but we always make our own wheels".

Each of them has been implemented through the browser's native implementation, and can run without polyfill. With one sentence leading to five sentences, simply copy and paste after reading can make colleagues exclaim, 'Is there still such an operation?'.

1. Array flattening: flat/flatMap

Scenario: The backend crams the tree structure into you, while the frontend only wants to take leaf nodes.

bonus:flatMap = map + flat(1), Complete the "one to many" mapping in one loop.

2. Object ↔  Array 'teleportation': entries ↔  fromEntries

Scenario: I only want to perform "filtering/mapping/sorting" on objects, but I am too lazy to write "reduce".

//img.enjoy4fun.com/news_icon/d3thb1ddm8bc72r49h9g.png

URL parsing can also be completed in one line:

//img.enjoy4fun.com/news_icon/d3thb8ddm8bc72r49ol0.png

3. String completion: padStart/padEnd

Scenario: Time, order, ID card, the digits must be aligned.

//img.enjoy4fun.com/news_icon/d3thbi5dm8bc72r4a3eg.png

Fixed Number:

//img.enjoy4fun.com/news_icon/d3thboofe6kc72sph960.png

4. Array deduplication+set operation: Set

Scenario: The interface returned 10000 pieces of data, of which 30% were duplicate IDs.

//img.enjoy4fun.com/news_icon/d3thc1ofe6kc72sphjog.png

5. Deconstruction of "nested+default values"

Scenario: Interface fields are often missing and need to be downgraded.

//img.enjoy4fun.com/news_icon/d3thc85dm8bc72r4at40.png//img.enjoy4fun.com/news_icon/d3thcptdm8bc72r4bfqg.png

Deep security value:

//img.enjoy4fun.com/news_icon/d3thceddm8bc72r4b350.png

6. Asynchronous "tail sweeping" artifact: terminal()

Scenario: Loading must be turned off when the request ends, and it must be turned off for both successful and failed requests.

Conclusion: How to painlessly develop a "new habit"?


The code review deliberately asked: Can flatMap/from Entries be used here?

Flag: Use these 8 APIs in the business for three consecutive weeks, and give yourself a ★ every time you use them.

Team sharing: Share this article in the group, who will use it in the PR latest, please have the whole group milk tea 🧋。

(Part 2): Unlocking infinite possibilities for modern web interaction

learn more

(Part 1): Understanding its core role in front-end development from scratch

learn more

AI driven R&D efficiency improvement: A practical guide for advanced developers

learn more

Are there actually so many methods on the console?

view Are there actually so many methods on the console?

🔍 Vue Hidden God Technique: 99% of developers haven't used it, but it can make code concise by 50%

learn more

Front end Technology Weekly Issue 93- ECharts 6.0, Vite Field News Summary Cursor 1.4...

view Front end Technology Weekly Issue 93- ECharts 6.0, Vite Field News Summary Cursor 1.4...

Frontend Permission Control: Interfaces, Routes, and Buttons

learn more

With so many backend frameworks in Node.js, which one should I use?

view With so many backend frameworks in Node.js, which one should I use?

Three major feature updates! Take you on a comprehensive understanding of NJET 4.0 version

learn more

The role and usage of the perf_fooks module in Node.js

view The role and usage of the perf_fooks module in Node.js

Front end debugging practice

learn more

Optimization Solution for Virtual Human Shadow Rendering Based on Three.js

view Optimization Solution for Virtual Human Shadow Rendering Based on Three.js

First Principles of Browser Crashes: The Art of Memory Management

learn more

5 TypeScript TypeScript Tools for Advanced Users

view 5 TypeScript TypeScript Tools for Advanced Users

[Frontier Analysis] What new features will JavaScript have in the future?

learn more