Background:
I have almost done the project requirements. I have nothing to do. I reviewed my own code and optimized some codes.
Practical examples:
One: twice traversal optimization
Before optimization:
The optimized
The main purpose here is to calculate the sum of integral and quantity in a returned data. Before, map was used to calculate the total integral of each object, and then forEach was used to calculate the sum of an array. But when you query the array method, you suddenly see the reduce of the array, which can execute a function on each object in the array and return a value. So you can make an optimization for code that has been traversed twice;
Two: multi-layer ifelse nesting optimization
Before optimization
This is the code before and after optimization
The optimized
Here is mainly on the Internet to learn a strategy pattern optimization method, all the form check out as a single file object, and through the way of traversing in page validation, the benefits of using some are as long as there is a do not conform to, terminate traversal, so more efficient, see an article with the interpretation of some First is to define a js file, The method of putting check
Import through importThen get the key of the form object, traverse through some method, pass the key and value into the verification method, if one of them does not match, return false, and toast prompt;
The following is a conversion of the input value of an input field, using if and then optimized to switch case; Eventually optimized to return the form of an object;