The content is basically this year from the article of other great gods to learn things. The exit is at the bottom.

1. Array.includes and conditional judgment

Generally we use judgment or | |

If we had more fruit

2, Set and deweight

ES6 provides a new data structure, Set. It is similar to an array, but the values of the members are unique and there are no duplicate values. The Set itself is a constructor used to generate the Set data structure.

Array to heavy


The array. from method converts a Set structure to an Array. We can write a special function that uses a de-weight function

Characters to heavy

In addition, Set is so powerful that it is easy to implement Union, intersection, and Difference using Set.

3. Map and dictionary data

In general, JavaScript implements dictionary data based on Object objects. But JavaScript objects can only have strings of keys. It’s a lot of inconvenience for programming. ES6 provides Map data structures. Similar to Object, it is also a collection of key-value pairs, but the range of “keys” is not limited to strings. Various types of values, strings, values, Booleans, arrays, objects, and so on, can be used as keys.

The traversal order of a Map is the insertion order

4. Handle data in a functional way

Functional programming, as I understand it, insists that a function must take at least one argument and return a value. So all operations on data can be handled in a functional way.

Let’s say we need to change the structure of the objects in array foo, then pick out the qualified objects and put them into a new array result.

It is intuitively easy to write code like this:

It’s much more elegant to write it as a function

Array sum

5. Compose with functions

The following code is called composite compose


Due to the popularity of functional programming, you’ll now see a lot of code with arrows ()=>()=>()=> in JavaScript code.

ES6 version compose


In the definition of compose, g will execute before f, so a right-to-left data flow is created. This is much more readable than nesting a bunch of function calls.

We take some functions, combine them, and create a brand new function.

Reverse reverses the list, head takes the first element in the list;


However, our compose is not perfect and can only handle two function parameters. The compose function is composed for redux.

With this function, we can combine an infinite number of functions at will. Now we add the requirement to create a lastAndUpper function that reverses the list, takes the first element in the list, and capitalizes toUpperCase.


Author: Liao Zhenting

The original link: https://zhuanlan.zhihu.com/p/54758068