Home > Web Front-end > JS Tutorial > JS array (Array) processing function sorting_basic knowledge

JS array (Array) processing function sorting_basic knowledge

WBOY
Release: 2016-05-16 16:28:38
Original
1609 people have browsed it

1. concat() connects two or more arrays
This method does not modify the existing array, but simply returns a copy of the concatenated array.
For example:

Copy code The code is as follows:


Output result:
1,2,3,4,5,11,22,33

2. join()
Put all elements of the array into a string. Elements are separated by the specified delimiter.
For example:

Copy code The code is as follows:


list results:

'

  • item 1
  • item 2
  • item 3
'
This is by far the fastest method! Using native code (such as join()), regardless of what the system does internally, is usually much faster than non-native. ——James Padolsey, james.padolsey.com

3. pop() deletes and returns the last element of the array
The pop() method will remove the last element of the array, decrement the array length by 1, and return the value of the element it removed.
If the array is already empty, pop() does not change the array and returns an undefined value
For example:

Copy code The code is as follows:


Output result:
George,John,Thomas
Thomas
George, John

4. push() adds one or more elements to the end of the array and returns the new length
For example:

Copy code The code is as follows:


Output result:
George,John,Thomas
4
George,John,Thomas,James

5. unshift() adds one or more elements to the beginning of the array and returns the new length
For example:

Copy code The code is as follows:


Output result:
George,John,Thomas
4
James,George,John,Thomas

6. reverse() reverses the order of elements in the array
For example:

Copy code The code is as follows:


Output result:
George,John,Thomas
Thomas,John,George

7. shift() deletes and returns the first element of the array
For example:

Copy code The code is as follows:


Output result:
George,John,Thomas
George
John,Thomas

8. slice(start,end) returns the selected element from an existing array
Please note that this method does not modify the array, but returns a subarray
For example:

Copy code The code is as follows:


Output result:
George,John,Thomas
John,Thomas
George,John,Thomas

9. sort() sorts the elements of the array
A reference to the array. Please note that the array is sorted on the original array and no copy is generated
By default, this method sorts according to the order of character encoding (ASCII)
For example:

Copy code The code is as follows:



Output result:
John,George,Thomas
George,John,Thomas

Let’s look at another example:

Copy code The code is as follows:



Output result:
10,5,40,25,1000,1
1,10,1000,25,40,5

We can see that it is not sorted by numerical size as we think. If you want to sort by numerical size, you need to change the default sorting method and specify the sorting rules yourself.
As follows:

Copy code The code is as follows:



Output result:
10,5,40,25,1000,1
1,5,10,25,40,1000
What if you want to sort in descending order?
Change the sort order to:
function (a, b) {return b - a;}
It’s OK

10. splice() deletes elements and adds new elements to the array
The splice() method has different functions from the slice() method. The splice() method will directly modify the array
(1) Delete array elements in the specified range:

Copy code The code is as follows:



Output result:
George,John,Thomas,James,Adrew,Martin
George,John,Martin

(2) Insert the specified element starting from the specified subscript (the number of elements is not limited):

Copy code The code is as follows:



Output result:
George,John,Thomas,James,Adrew,Martin
George,John,William,JACK,Thomas,James,Adrew,Martin


(3) Delete the array elements in the specified range and replace them with the specified elements (the number of elements is not limited):

Copy code The code is as follows:



Output result:
George,John,Thomas,James,Adrew,Martin
George,John,William,JACK,Martin
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template