There are still many array operation functions in js. I suddenly thought of summarizing it today, which can be regarded as reviewing the past and learning the new. However, we will not summarize each method, but only make notes for some more commonly used ones.
The js array operation functions summarized here are: push, pop, join, shift, unshift, slice, splice, concat
(1) push and pop
Both functions perform pressing or popping operations on the array from the tail. push(arg1,arg2,...) can press one or more elements at a time and return the updated array length. Note that if the parameters are also arrays, then all arrays are pressed into the original array as an element. The pop() function will only pop up the ending element at a time and return the popped element. If pop() is called on an empty group, it will return undefined.
Example:
var oldArr=[1,2,3];
alert(oldArr.push(4,[5,6]))//Here, [5,6] will only be used as an element to plan, and return the updated array length 5
At this time oldArr = [1,2,3,4,[5,6]]
oldArr.pop()//The last element [5,6] pops up here, not 6
At this time oldArr = [1,2,3,4]
oldArr.pop()-->4
oldArr.pop()-->3
oldArr.pop()-->2
oldArr.pop()-->1
alert(oldArr.pop())-->undefined(empty array pops up)
(2) unshift and shift
The unshift() method adds one or more elements to the beginning of the array and returns a new length. The unshift() method will insert its argument into the head of the arrayObject and move existing elements sequentially to the higher subscript to leave space. The first parameter of the method will become the new element 0 of the array, if there is a second parameter, it will become the new element 1, and so on.
Please note that the unshift() method does not create a new creation, but directly modifies the original array. In addition, unshift() cannot be executed in the Internet Explorer browser!
In the following example, we will create an array and add an element to the beginning of the array and return the new length of the array:
<script type="text/javascript">var arr = new Array()arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr.join() + "<br />")document.write(arr.unshift("William") + "<br />")document.write(arr.join())</script>Output:
George, John, Thomas
4
William George John Thomas
shift() is used to delete the first element of the array from the original array and return the value of the first element (i.e. the value of the deleted element).
Note: If the array is empty, shift() will not perform any manipulation and will directly return the undefined value. In addition, this method does not create a new array, but directly modify the original arrayObject.
Example: In this example, we will create an array and delete the first element of the array:
<script type="text/javascript">var arr = new Arrayarr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr.join() + "<br />")document.write(arr.shift() + "<br />")document.write(arr.join())</script>
Output:
George, John, Thomas
George
John, Thomas
(3) join()
The function is to concatenate each element of the array into a string through the specified delimiter. Its function is the same as toString().
grammar
arrayObject.join(separator)
Parameter separator is optional. Specifies the separator to use. If this parameter is omitted, a comma is used as the separator.
Example:
var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr.join())
Output
George, John, Thomas
(4) slice()
This method returns the selected element from the existing array
grammar
arrayObject.slice(start,end)
Return value
Returns a new array containing elements in an arrayObject from start to end (excluding this element).
Note: You can select elements from the tail of the array using negative values. If end is not specified, the slice() method selects all elements from start to the end of the array.
Example:
<script type="text/javascript">var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"document.write(arr.join() + "<br />")document.write(arr.slice(1) + "<br />")document.write(arr.join())</script>
Output:
George, John, Thomas
John Thomas
George, John, Thomas
(5) splice()
This method is used to insert, delete, or replace elements of an array.
grammar
arrayObject.splice(index,howmany,element1,......,elementX)
Return value
If an element is deleted from the arrayObject, an array containing the deleted elements is returned.
illustrate
The splice() method deletes zero or more elements starting at index and replaces those deleted elements with one or more values declared in the parameter list. It should be noted that the function of the splice() method is different from the slice() method. The splice() method will directly modify the array.
Example:
Example 1
In this example, we will create a new array and add an element to it:
<script type="text/javascript">var arr = new Array(6)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"arr[3] = "James"arr[4] = "Adrew"arr[5] = "Martin"document.write(arr.join() + "<br />")arr.splice(2,0,"William")document.write(arr.join() + "<br />")</script>
Output:
George, John, Thomas, James, Adrew, Martin
George, John, William, Thomas, James, Adrew, Martin
Example 2
In this example we will delete the element located in index 2 and add a new element to replace the deleted element:
<script type="text/javascript">var arr = new Array(6)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"arr[3] = "James"arr[4] = "Adrew"arr[5] = "Martin"document.write(arr.join() + "<br />")arr.splice(2,1,"William")document.write(arr.join())</script>
Output:
George, John, Thomas, James, Adrew, Martin
George, John, William, James, Adrew, Martin
Example 3
In this example we will delete the three elements starting with index 2 ("Thomas") and add a new element ("William") to replace the deleted element:
<script type="text/javascript">var arr = new Array(6)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"arr[3] = "James"arr[4] = "Adrew"arr[5] = "Martin"document.write(arr.join() + "<br />")arr.splice(2,3,"William")document.write(arr.join())</script>
Output:
George, John, Thomas, James, Adrew, Martin
George, John, William, Martin
(6) contact()
This method is used to concatenate two or more arrays. It does not change the existing array, but only returns a copy of the array being joined.
grammar
arrayObject.concat(arrayX,arrayX,......,arrayX)
Example:
Example 1
In this example, we will concatenate the parameters in concat() into array a:
<script type="text/javascript">var a = [1,2,3];document.write(a.concat(4,5));</script>
Output:
1,2,3,4,5
Example 2
In this example, we create two arrays and then use concat() to concatenate them:
<script type="text/javascript">var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"var arr2 = new Array(3)arr2[0] = "James"arr2[1] = "Adrew"arr2[2] = "Martin"document.write(arr.concat(arr2))</script>
Output:
George, John, Thomas, James, Adrew, Martin
Example 3
In this example, we create three arrays and then use concat() to concatenate them:
<script type="text/javascript">var arr = new Array(3)arr[0] = "George"arr[1] = "John"arr[2] = "Thomas"var arr2 = new Array(3)arr2[0] = "James"arr2[1] = "Adrew"arr2[2] = "Martin"var arr3 = new Array(2)arr3[0] = "William"arr3[1] = "Franklin"document.write(arr.concat(arr2,arr3))</script>
Output:
George, John, Thomas, James, Adrew, Martin, William, Franklin
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.