In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "sharing practical skills of JS arrays". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Delete duplicates from an array
two。 Replace a specific value in an array
Sometimes you need to replace specific values in an array when creating code, and there is a good short way to do this. We can use .splice (start, value to remove, valueToAdd), these parameters specify where we want to start, how many values we want to modify, and replace new values.
3. Array.from achieves the effect of .map
We all know that the .map () method and the .map () method can be used to achieve a similar effect and the code is concise.
4. Empty array
Sometimes we need to empty the array, and a quick way to do this is to let the array's length property be 0.
5. Convert an array to an object
Sometimes, for some purpose, you need to convert an array into an object, and a simple and quick way is to use the expansion operation symbol (.):
6. Populate the array with data
In some cases, the .fill () method can help us when we create an array and want to populate it with some data.
7. Array merging
You can also combine multiple numbers using the unfold operator.
8. Find the intersection of two arrays
Finding the intersection of two arrays is also a difficult point in the interview. in order to find the intersection of two arrays, we first use the above method to ensure that the values in the checked array are not duplicated, and then use the .filter method and the .filter method. As follows:
9. Remove an imaginary value from an array
In JS, the imaginary values are false, 0jime, null, NaN, undefined. We can filter these virtual values with the .filter () method.
10. Get random values from an array
Sometimes we need to randomly select a value from the array. A convenient way is to get a random index based on the length of the array, as follows:
11. Reverse array
Now, when we need to reverse the array, there is no need to create it through complex loops and functions, the reverse method of the array can do it:
12 lastIndexOf () method
13. Sum all values in an array
Reduce method is often used to solve problems skillfully in JS interview.
This is the end of the content of "practical skills sharing of JS arrays". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.