In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the JavaScript array skills", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the JavaScript array skills?" this article.
Array is one of the most common concepts in Javascript. It provides us with many possibilities for dealing with data, and it is necessary to be familiar with some common operations of arrays.
Array deduplication 1, from () superposition new Set () method
The from method can be used directly for the deduplication of a string or numeric array.
Var plants = ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Earth', 'Mars',' Jupiter']; var uniquePlants = Array.from (new Set (plants)); console.log (uniquePlants); / / ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Mars', 'Jupiter'] 2, spread operator (…)
The extension operator is a major innovation of ES6, and there are many powerful features.
Var plants = ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Earth', 'Mars',' Jupiter']; var uniquePlants = [... new Set (plants)]; console.log (uniquePlants); / / ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Mars', 'Jupiter'] replace specific values in the array
The splice () method adds / removes items to / from the array, and then returns the deleted items. This method changes the original array. Special attention should be paid to the position of the insertion value!
/ / arrayObject.splice (index,howmany,item1,.,itemX) var plants = ['Saturn',' Uranus', 'Mercury',' Venus', 'Earth',' Mars', 'Jupiter']; var result = plants.splice (2,1,' www.shanzhonglei.com') console.log (plants) / / ['Saturn','Uranus','www.shanzhonglei.com','Mercury','Venus','Earth','Mars','Jupiter'] console.log (result); / / [' Mercury'] No mapping array of map ()
Let's first introduce the map method. The map () method returns a new array in which the elements are the values of the original array elements after calling the function, and it processes the elements in turn in the order of the original array elements. Note: map () does not change the original array, nor does it detect empty arrays.
Let's implement an array mapping without map:
/ array.map (function (currentValue,index,arr), thisValue) var plants = [{name: "Saturn"}, {name: "Uranus"}, {name: "Mercury"}, {name: "Venus"},] var plantsName = Array.from (plants, ({name}) = > name); console.log (plantsName); / / [Saturn', 'Uranus',' Mercury', 'Venus'] empty array
If you want to empty an array, just set the length of the array to 0. well, this is a little simple.
Var plants = ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Earth', 'Mars',' Jupiter']; plants.length = 0 position console.log (plants); / / [] convert array to object
If you want to convert an array to an object, the fastest way is the spread operator (...).
Var plants = ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Earth', 'Mars',' Jupiter']; var plantsObj = {... plants} console.log (plantsObj) / / {'0entries:' Saturn','1': 'Earth',' 2percent:' Uranus','3': 'Mercury','4':' Venus','5': 'Earth','6':' Mars','7': 'Jupiter'} populates the array with data
If we need to populate the array with some data, or if we need data with the same value, we can use the fill () method.
Var plants = new Array (8). Fill ('8'); console.log (plants); / / merge arrays.
Of course you'll think of the concat () method, but, oh, the spread operator (...) It also smells good, and this is another application of the extension operator.
Var plants1 = ['Saturn',' Earth','Uranus', 'Mercury']; var plants2 = [' Venus', 'Earth','Mars',' Jupiter']; console.log ([... plants1,... plants2]); / / the intersection of two arrays ['Saturn',' Earth','Uranus', 'Mercury','Venus',' Earth','Mars', 'Jupiter']
To require the intersection of two arrays, first make sure that the array does not repeat, and then use the filter () method and the includes () method.
Var plants1 = ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Earth', 'Mars',' Jupiter']; var plants2 = ['Saturn',' Earth', 'Uranus']; var alonePlants = [. New Set (plants1)] .filter (item = > plants2.includes (item)); console.log (alonePlants); / / [Saturn',' Earth', 'Uranus'] delete false values in the array
We often need to remove false values when dealing with data. In Javascript, false values are false, 0, ", null, NaN, undefined.
Var plants = ['Saturn',' Earth', null, undefined, false, ",", NaN, 'Uranus',' Mercury','Venus', 'Earth','Mars',' Jupiter']; var trueArr = plants.filter (Boolean); console.log (trueArr); / / ['Saturn',' Earth','Uranus', 'Mercury','Venus',' Earth','Mars', 'Jupiter'] to get random values in the array
We can get a random index number based on the length of the array.
Var plants = ['Saturn',' Earth', 'Uranus',' Mercury', 'Venus',' Earth', 'Mars',' Jupiter']; console.log (plants [Math.trees (Math.random () * (plants.length + 1))]) lastIndexOf () method
LastIndexOf () can help us find the index of the last occurrence of the element.
/ / array.reduce (function (total, currentValue, currentIndex, arr), initialValue) var nums = [1, 2, 3, 4, 5]; var sum = nums.reduce ((x, y) = > x + y); console.log (sum); / / 15 adds all values in the array
The reduce () method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value.
/ / array.reduce (function (total, currentValue, currentIndex, arr), initialValue) var nums = [1, 2, 3, 4, 5]; var sum = nums.reduce ((x, y) = > x + y); console.log (sum); / / 15 is all the contents of the article "what are the array techniques used by JavaScript"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.