Array is JS The most common data structure , We often use it in development , In this article , Here are some tips , Help us improve development efficiency .

1. Remove duplicate items from array

2. Replace a specific value in an array

Sometimes you need to replace a specific value in an array when you create code , There is a good short way to do this , We can use it .splice(start,value to
remove,valueToAdd), These parameters specify where we want to start , How many values are modified and replaced with new values .

3. Array.from achieve .map The effect of

We all know that  .map()  method ,.from()  Methods can also be used to achieve similar results, and the code is concise .

4. Empty array

Sometimes we need to empty the array , A quick way is to directly let the array  length  Property is  0, You can empty the array .

5. Convert array to object

occasionally , For some purpose , You need to convert the array to an object , A simple and fast way is to use the expansion operation symbol (...):

6. Fill the array with data

In some cases , When we create an array and want to fill it with some data , At this time  .fill() Methods can help us .

7. Array merging

Using the expand operator , You can also combine multiple arrays .

8. Find the intersection of two arrays

Finding the intersection of two arrays is also a difficult point in the interview , To find the intersection of two arrays , First, use the above method to ensure that the values in the checked array are not repeated , Then use .filter  Methods and
.includes method . As shown below :

9. Remove virtual values from array

stay JS in , Virtual value has  false, 0,'', null, NaN, undefined. We can  .filter()  Method to filter these virtual values .

10. Get random values from an array

Sometimes we need to select an array of random values . A convenient way is to get a random index according to the length of the array , As shown below :

11. Invert array

Now? , When we need to reverse the array , There is no need to create it through complex loops and functions , Array of  reverse  The method can be done :

12 lastIndexOf() method

13. Sums all values in an array

JS It's often used in interviews  reduce  Methods to solve problems skillfully


In this paper , Introduced 13 Tips , Hopefully, they can help write concise code , If you have a better way , Welcome to leave a message for discussion .

©2019-2020 Toolsou All rights reserved,
How to prevent Mac put to sleep ? Tell you 4 This is an effective way be based on C# Students' grades / mis solve element-ui The drop-down box of has a value but cannot be selected Student information management system source code -C language VR,AR and MR The difference between these technologies 【 Data structure and algorithm 6】 Stack implementation of comprehensive calculator utilize python Yes monkey Automatic log analysis Laya Wechat games sub package loading Alibaba enterprise email smtp set up ( practice )【Java Details of knowledge points 3】 Serializable and Deserialize