Like it and see. Make it a habit

In this paper, making github.com/qq44924588… Has included more categories of previous articles, as well as a lot of my documentation and tutorial material. Welcome Star and Perfect, you can refer to the examination points for review in the interview, I hope we can have something together.

Array is the most common JS data structure, we often use in the development, in this article, to provide some tips to help us improve the efficiency of development.

1. Delete the duplicates of the array

2. Replace specific values in the array

Splice (start, Value to remove, valueToAdd). These parameters specify where we want to modify from, how many values to modify, and replace the new values.

3. Array.from achieves the effect of.map

We all know that the.map() and.from() methods can be used to achieve similar results and the code is very concise.

4. Empty the array

Sometimes we need to empty an array. A quick way to do this is to set the length property of the array to 0.

5. Convert arrays to objects

Sometimes, when you need to convert an array to an object for some purpose, a quick and easy way is to use the expansion notation (…). :

6. Populate the array with data

In some cases, when we create an array and want to fill it with data, the.fill() method can help.

7. Array merge

Multiple arrays can also be merged using the expansion operator.

8. Find the intersection of two arrays

To find the intersection of two arrays, first use the above method to ensure that the values in the array are not duplicated, then use the.filter method and.includes method. As follows:

9. Remove the virtual value from the array

In JS, virtual values are false, 0, “, null, NaN, undefined. We can use the.filter() method to filter these virtual values.

10. Get random values from arrays

Sometimes we need to randomly select a value from an array. A convenient way to do this is to get a random index based on the array length, as shown below:

11. Reverse the array

Now, when we need to reverse an array, we don’t need to create it through complicated loops and functions. The reverse method does just that:

12 lastIndexOf () method

13. Sum all the values in the array

JS interview also often use reduce method to solve problems skillfully

conclusion

In this article, I’ve covered 13 tips that I hope will help you write clean code, and if you have a better idea, feel free to leave a comment.

The bugs that may exist after code deployment cannot be known in real time. In order to solve these bugs, I spent a lot of time on log debugging. Incidentally, HERE I recommend a good BUG monitoring tool Fundebug.