JavaScript array operation function Summary _javascript tips

Source: Internet
Author: User
Tags array length arrays javascript array

JS in the array operation function is still very much, today suddenly thought to sum up, is also known as the temperature of the new bar. However, will not be for each approach to a summary, but for some more commonly used to make a note.
The JS array operation functions summarized here are: Push,pop,join,shift,unshift,slice,splice,concat
(1) Push and pop
/span> These two functions are the array from the tail to press in or eject operation. Push (Arg1,arg2,...) You can press one or more elements at a time and return the updated array length. Note If the argument is also an array, then the entire array is pressed into the original array as an element. The pop () function only pops the ending element at a time and returns the element that pops up, and returns undefined if the pop () is invoked on the empty group number.
Example:
var oldarr=[1,2,3];
Alert (Oldarr.push (4,[5,6))//[5,6] will only be used as an element, returning the updated array length 5
at this time Oldarr = [ 1,2,3,4,[5,6]]
Oldarr.pop ()//Here pops up the last element [5,6] instead of 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 popup)

(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 inserts its arguments into the head of the Arrayobject and moves the existing elements to a higher subscript in order to allow space. The first argument of the method becomes the new element 0 of the array, and if there is a second argument, it becomes the new element 1, and so on.
Note that the Unshift () method does not create a new creation, but instead modifies the original array directly. In addition, unshift () cannot be performed in the Internet Explorer browser!
As 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] = "T Homas "
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 remove the first element of an 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, then shift () will not be manipulated to return the undefined value directly. In addition, the 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] = "Tho Mas "
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 effect is to concatenate the elements of an array by a specified delimiter into a string. The effect is the same as ToString ().
Grammar
Arrayobject.join (separator)
Parameter separator Optional. Specifies the separator character to use. If this argument is omitted, commas are used as delimiters.
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 ()
The method can return the selected element from an existing array
Grammar
Arrayobject.slice (Start,end)
return value
Returns a new array containing the elements in the arrayobject from start to end (excluding the element).
Note: You can use negative values to select elements from the tail of the array. If end is not specified, then the slice () method selects all elements from start to ending 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 removed from the Arrayobject, an array containing the deleted elements is returned.
Description
The splice () method deletes 0 or more elements starting at index, and replaces the deleted elements with one or more values declared in the argument list. It is important to note that the splice () method is different from the slice () method, and the splice () method is modified directly to the array.
Example:
Example 1
In this case, we'll 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 at 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 connect two or more arrays. It does not change an existing array, but simply returns a copy of the connected array.
Grammar
Arrayobject.concat (Arrayx,arrayx,......, Arrayx)
Example:
Example 1
In this case, we'll connect the arguments in concat () to 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 case, we created two arrays and then connected 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"
docum Ent.write (Arr.concat (arr2))
</script>

Output:
George,john,thomas,james,adrew,martin
Example 3
In this case, we created three arrays and then connected 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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.