JavaScript Array Operation Function Summary

  • 2021-07-07 06:33:09
  • OfStack

js array operation function is still very much, today suddenly thought to sum up 1, is also a review of the past and know the new bar. However, I will not summarize each method, but make a remark on some 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 press or eject 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 parameter is also an array, the whole array is pressed into the original array as one element. The pop () function will only pop up the ending element and return the pop-up element at a time. If pop () is called on the number of empty groups, it will return undefined.
Example:
var oldArr=[1,2,3];
alert (oldArr. push (4, [5, 6]))//Here [5, 6] will only be planned as one element, and the updated array length is 5
oldArr = [1, 2, 3, 4, [5, 6]]
oldArr. pop ()//The last element [5, 6] pops up here instead of 6
oldArr = [1, 2, 3, 4]
oldArr.pop()-- > 4
oldArr.pop()-- > 3
oldArr.pop()-- > 2
oldArr.pop()-- > 1
alert(oldArr.pop())-- > undefined (Empty Array Popup)

(2) unshift and shift
The unshift () method adds one or more elements to the beginning of an array and returns the new length. The unshift () method inserts its arguments into the arrayObject header and moves the existing elements sequentially to the higher subscripts to make room. The first parameter of this method will become the new element 0 of the array, and if there is a second parameter, it will become the new element 1, and so on.
Note that the unshift () method does not create a new creation, but directly modifies the original array. In addition, unshift () cannot be executed in Internet Explorer browser!
In the following example, we will create an array, 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 (that is, the value of the deleted element).
Note: If the array is empty, shift () returns the undefined value without any manipulation. In addition, this method does not create a new array, but directly modifies 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 Array
arr[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 connect each element of the array into a string through the specified separator. Its function is the same as that of toString ().
Grammar
arrayObject.join(separator)
The 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 an existing array
Grammar
arrayObject.slice(start,end)
Return value
Returns a new array containing elements from arrayObject from start to end (excluding this element).
Note: You can use negative values to select elements from the end of an array. 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 arrayObject, an array containing the deleted element is returned.
Description
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. Note that the splice () method is different from the slice () method, and the splice () method modifies the array directly.
Example:
Example 1
In this example, we will create a new array and add one 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 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 connect two or more arrays. It does not change the existing array, but only returns 1 copy of the connected array.
Grammar
arrayObject.concat(arrayX,arrayX,......,arrayX)
Example:
Example 1
In this example, we will concatenate the parameters in concat () to the 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 join them using concat ():


<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 join them using concat ():


<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


Related articles: