Note: This article is from W3school Chinese network, if need full version please go to--http://www.w3school.com.cn/jsref/jsref_obj_array.asp
The 1.1 Slice () method ---Returns the selected element from an existing array.
Arrayobject.slice (start,end)//Syntax
Start Required. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
End is optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array.
Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.
Note that the method does not modify the array, but instead returns a subarray. If you want to delete an element from an array, you should use Method Array.splice ().
1<script type= "Text/javascript" >2 3 vararr =NewArray (3)4Arr[0] = "George"5ARR[1] = "John"6ARR[2] = "Thomas"7 8document.write (arr + "<br/>")//George,john,thomas9document.write (Arr.slice (1) + "<br/>")//John,thomasTendocument.write (arr)//George,john,thomas One A</script>
In this example, we will create a new array and then display the elements selected from it:
1<script type= "Text/javascript" >2 3 vararr =NewArray (6)4Arr[0] = "George"5ARR[1] = "John"6ARR[2] = "Thomas"7ARR[3] = "James"8ARR[4] = "Adrew"9ARR[5] = "Martin"Ten Onedocument.write (arr + "<br/>")//George,john,thomas,james,adrew,martin Adocument.write (Arr.slice (2,4) + "<br/>")//Thomas,james -document.write (arr)//George,john,thomas,james,adrew,martin - the</script>
The 1.2 sort () method ---Used to sort the elements of the array.
Grammar Arrayobject.sort (SortBy)
SortBy is optional. Specifies the sort order. Must be a function.
Example
1<script type= "Text/javascript" >2 3 vararr =NewArray (6)4Arr[0] = "George"5ARR[1] = "John"6ARR[2] = "Thomas"7ARR[3] = "James"8ARR[4] = "Adrew"9ARR[5] = "Martin"Ten Onedocument.write (arr + "<br/>")//George,john,thomas,james,adrew,martin Adocument.write (Arr.sort ())//Adrew,george,james,john,martin,thomas - -</script>
In this example, we will create an array and sort alphabetically:
<script type= "Text/javascript" >varnew Array (6) arr[0] = "Ten"arr[1] = "5" arr[2] = "arr[3" = "arr[" (4] = "1"arr[5] = ""+ "<br/>")/ /10,5,40,25,1000,1document.write (Arr.sort ())//1,10,1000,25,40,5</ Script>
Note that the above code does not sort numbers by numeric values, and to do this, you must use a sort function:
1<script type= "Text/javascript" >2 3 functionSortnumber (A, b)4 {5 returnAb6 }7 8 vararr =NewArray (6)9Arr[0] = "10"TenARR[1] = "5" OneARR[2] = "40" AARR[3] = "25" -Arr[4] = "1000" -ARR[5] = "1" the -document.write (arr + "<br/>")//10,5,40,25,1000,1 -document.write (Arr.sort (Sortnumber))//1,5,10,25,40,1000 - +</script>
1.4 Splice () method --adds/deletes an item to/from the array, and then returns the item that was deleted.
Arrayobject.splice (index,howmany,item1,....., ItemX)
Index required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany required. The number of items to delete. If set to 0, the item is not deleted.
Item1, ..., itemx Optional. Adds a new item to the array.
return value
Array contains the new array of deleted items, if any.
Description
Description: The Splice () method removes 0 or more elements starting at index and replaces those that were deleted with one or more values declared in the argument list.
If the element is removed from the Arrayobject, the array containing the deleted element is returned.
In this example, we will create a new array and add an element to it:
<script type= "Text/javascript" >varnew Array (6) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "arr[3] =" James "arr[4] =" Adrew "arr[5] =" Martin "+" <br/> ")//george,john,thomas,james,adrew,martinarr.splice (2,0," William "+" <br/> ")//george,john,william,thomas,james,adrew,martin</script>
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" >varnew Array (6) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "arr[3] =" James "arr[4] =" Adrew "arr[5] =" Martin "+" <br/> ")//george,john,thomas,james,adrew,martinarr.splice (2,1," William ") ) document.write (arr)//george,john,william,james,adrew,martin</script>
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" >varnew Array (6) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "arr[3] =" James "arr[4] =" Adrew "arr[5] =" Martin "+" <br/> ")//george,john,thomas,james,adrew,martinarr.splice (2,3," William ") document.write (arr)//george,john,william,martin</script>
1.5 toString () method --Converts an array to a string and returns the result.
Grammar arrayobject.tostring ()
<script type= "Text/javascript" >varnew Array (3) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "document.write (arr.tostring ())//George,john,thomas </script>
1.5 unshift () method --You can add one or more elements to the beginning of the array and return the new length.
arrayobject.unshift (Newelement1,newelement2,...., newelementx)
Newelement1 required. The first element to add to the array. The
Newelement2 is optional. Adds a second element to the array. The
Newelementx is optional. You can add a number of elements.
Note that the Unshift () method does not create a new creation, but instead modifies the original array directly.
In this 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" >varnew Array () arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "+" <br/> ")//george,john,thomasdocument.write ( Arr.unshift ("William") + "<br/>")//4document.write (arr)//William,george, John,thomas</script>
Arry () array of understanding and use of APIs (ii)