In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the JavaScript array, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The method of adding elements to an array: 1.Array.push (value1,value2,... )
Append to the end of the array
The return value is the new length of the array after the data is added.
Will change the original array
Let arr = [1je 2jue 3]; let result= arr.push (4je 5); console.log (result) / / 5console.log (arr) / / [1je 2je 3m 4je 5] 2.Array.unshift (value1,value2, …)
Add to the beginning of the array
The return value is the new length of the array after the data is added.
Will change the original array
Let arr = [1Jing 2jue 3]; let result= arr.unshift (4je 5); console.log (result) / / 5console.log (arr) / / [4je 5pm 1m 2m 3] 3.splice (index,0,value1,value2, …)
Insert to the specified index of the array
The deleted element is returned.
Will change the original array
If only two parameters are passed in, it is to delete the number of [parameters 2] from the position of [parameter 1] let arr = [1jue 2jue 3]; let result = arr.splice (2je 0prim 4pr 5); console.log (result) / / Array (0) console.log (arr) / / [1pr 2pr 4p 5p 3] to delete elements to the array: 1.pop ()
Delete an element from the tail
Returns the deleted element.
Will change the original array
Let arr = [1Jing 2 Jue 3]; let result = arr.pop (); console.log (result) / / 3console.log (arr) / / [1JI 2] 2.shift ()
Delete an element from the header
Returns the deleted element.
Will change the original array
Let arr = [1Jing 2jue 3]; let result = arr.shift (); console.log (result) / / 1console.log (arr) / / [2Jing 3] 3.splice (index,howmany)
Delete howmany elements at index
Returns a collection of deleted elements.
Will change the original array
Let arr = [1Jing 2jue 3]; let result = arr.splice (1JI 2); console.log (result) / / [2JI 3] console.log (arr) / / [1] method of array sorting: 1.reverse ()
Inversion
This method changes the original array.
Let arr = [1Jing 2jue 3]; let result = arr.reverse (); console.log (result) / / [3JEI 2JEI 1] console.log (arr) / / [3JEI 2JEI 1] 2.sort ()
Sort by specified rules
Change the original array.
Let arr = [2Jing 1jue 3]; let result = arr.sort (); console.log (result) / / [1JEI 2jue 3] console.log (arr) / / [1Met 2je 3]
Note: if you think that the sort method is to sort the array from small to large, you are wrong. We can change the array and add some larger numbers.
Let arr = [25148,12recovery6jing38]; let result = arr.sort (); console.log (result) / / [12,148,25,38,6] console.log (arr) / / [12,148,25,38,6]
Are some of my friends a little confused now? But we can still find some rules, that is, he first compares according to the size of the first character of a number, and then uses the second character to compare, with the small one in front. If friends want this array to be sorted from really small to really large, then we can modify the code just now.
Let arr = [25, 14, 48, 12, 14, 14, 12, 12, 14, 14, 14, 12, 14, 14, 14, 5, 5, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 6, 12, 25, 38,148] console.log (result) / / [6,12,25,38,148] console.log (arr) / / [6,12,25,38,148]
If you want to sort from big to small, then
Let arr = [25,38,25,12jue 38]; let result = arr.sort ((Agraine b) = > b-a); console.log (result) / / [148,38,25,12jue 6] console.log (arr) / / [148,38,25,12jue 6]
In general, the parameter in the sort method of the array is a callback function with two values. If the return value is the first value minus the second value, it is sorted from smallest to largest, and if the second value is subtracted from the first value, it is sorted from largest to smallest.
Array join method: 1.concat ()
Array join
Returns the new array after the join.
Let arr = [25 result 148] let result = arr.concat (arr2); console.log (result) / / [25 result) / / [25Petre 148 12jue 6je 38] 2.join ()
Concatenates each element of the array into characters with a specified delimiter (default is ",")
Returns the string after the connection is completed.
Let arr = [25 (+') console.log (result) / / let result = arr.join (), result2 = arr.join ('+') console.log (result) / / 25Phen148 console.log / / the method of intercepting the array: 1.slice (start,end)
From the start start index to the end end index
Returns the intercepted collection of elements. Note: if you do not pass a value, then you are directly copying the values of the array. End may not pass a value, indicating that it is truncated to the end of the array. Start and end can also take negative values to indicate counting from back to front.
Let arr = [1 arr.slice (), result2 = arr.slice (2), result3 = arr.slice (2)]; Console.log (result) / / [1,2,3,4,5,6,7] console.log (result2) / / [3,4,5,6,7] console.log (result3) / / [3,4] console.log (arr) / / [1,2,3,4,5,6,7] Array conversion method: 1.toString ()
Convert to a string, which is consistent with join (), which does not pass an argument, and can also be converted to a different result using the hexadecimal system of 2meme.
Let arr = [1, console.log, 2, 3, 4, 4, 5, 5, and 6]; let result = arr.toString (); console.log (result) / / 1, 2, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6, 5, 5, 5, 5, 6] method of extracting specified depth from an array.
For example, there is now an array like this: [1Jing 2, [3je 4, [5je 6]], and I want to output it as [1m 2, 3pm 3, 4, 5, 6]. It's troublesome to write recursion by yourself. Then we can use the flat method of the array itself for deep parsing. 1.flat ()
Recursively traverse the array at the specified depth
The return value is the collection of all elements traversed.
Let arr = [1jue 2, [3jue 4, [5je 6]], res = arr.flat (Infinity), / / infinitely recursive until finally res2 = arr.flat () / / if no parameters are passed, then only recursively console.log (arr) / / [1 arr 2, [3 res 4, [5 recorder 6]] console.log (res) / / [1 recorder 2, [5 paramel 6] console.log (res2) / / [1 pint 2 pint 3 pint 4, [5pr 6]] above are all the contents of this article "how to use the array of JavaScript". Thank you for your 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.