<!doctype html> <meta charset= "UTF-8" <title > Array method </title> <body> <script type= "Text/javascript" //concat () method is used to connect two or more arrays. The //method does not change an existing array, but simply returns a copy of the connected array. //arrayobject.concat (Arrayx,arrayx,......, arrayx) //arrayx required. The parameter can be a specific value, or it can be an array object. Can be any number of. var a = [1,2,3]; var B = a.concat (4,5); var c = [' Jack ', ' Jonhn ', ' Tom ']; var d = [' Jim ', ' 111cn.net ']; var e = c.concat (d); document.write (A + <br/>);//1,2,3 document.write (b + "<br/>");//1,2,3,4,5 document.write (E + "<br/>"); The join () method is used to put all elements in an array into a string. The elements are delimited by the specified delimiter. Arrayobject.join (separator) Separator optional. Specifies the separator character to use. If this argument is omitted, commas are used as delimiters. var a1 = [1,2,3]; document.write ("Join Method <br/>" +a1.join () + "<br/>"); var B1 = [1,2,3]; document.write (B1.join (".") + "<br/>"); Use delimiters to separate elements in an array, outputting 1.2.3 document.write (The return value type of join () is: "+typeof (A1.join ()) +" <br/> "); The Pop () method is used to delete and return the last element of the array. Arrayobject.pop () Create an array, and then delete the last element of the array. Note that this also changes the extent of the array: var a2 = [1,2,3]; var b2 = A2.pop (); document.write (b2+ "<br/>"); Returns the last element of an array 3 document.write (a2+ "<br/>"); The original array has been changed to remove the last element and return to 1,2 The push () method adds one or more elements to the end of the array and returns a new length. Arrayobject.push (Newelement1,newelement2,...., newelementx) Newelement1 required. The first element to add to the array. Newelementx Optional. Multiple elements can be added. var a3 = [1,2,3]; var B3 = A3.push ("111cn.net"); document.write (b3+ "<br/>"); Output the new array length 4 document.write (a3+ "<br/>"); The original array has changed, 1,2,3,111cn.net The reverse () method reverses the order of elements in an array. Arrayobject.reverse () This method changes the original array without creating a new array. var a4 = [1,2,3]; var b4 = a4.reverse (); document.write (b4+ "<br/>"); Array Reverse 3,2,1 document.write (a4+ "<br/>"); Array has changed, and a4 = = B4; document.write (a4 = = = B4); True The shift () method deletes the first element of an array and returns the value of the first element. Arrayobject.shift () The value of the original first element of the array. If the array is empty, the shift () method does nothing, returning the undefined value. Note that the method does not create a new array, but instead directly modifies the original arrayobject. var a5 = [1,2,3]; var B5 = a5.shift (); document.write ("<br/>"); document.write (b5+ "<br/>"); Returns the first 1 document.write (a5+ "<br/>"); The array A5 has changed, returning to remove the first 2,3 The Unshift () method adds one or more elements to the beginning of the array and returns a new length. It's just the opposite of the order added to Pushu. Arrayobject.unshift (Newelement1,newelement2,...., newelementx) Newelement1 required. The first element added to the array. Newelementx Optional. You can add several elements. IE 7 and below do not work correctly var a6 = [' A ', ' B ', ' C ']; var b6 = a6.unshift (' d '); document.write (b6+ "<br/>"); Returns the new length 4 document.write (a6+ "<br/>"); The original array has been changed, the output d,a,b,c The slice () method returns the selected element from an existing array. Arrayobject.slice (Start,end) Start Required. Specify where to start the selection. If it is a negative number, then it sets the position from the end of the array. In other words,-1 refers to the last element, 2 to the penultimate element, and so on. End is optional. Specify where to end the selection. The parameter is the array subscript at the end of the array fragment. If this argument is not specified, the Shard array contains all the elements from start to the end of the array. If this argument is a negative number, it sets the element that starts at the end of the array. Returns a new array containing the elements in the arrayobject from start to end (excluding the element). Does not change the original array, but instead returns a child array var a7 = [' A ', ' B ', ' C ']; var b7 = A7.slice (1,3); document.write (b7+ "<br/>"); Returns the new array b,c document.write (a7+ "<br/>"); The original array is unchanged A,B,C The splice () method adds/deletes items to/from the array, and then returns the items that were deleted. This method changes the original array. Arrayobject.splice (index,howmany,item1,....., itemx) Index required. Integer that stipulates where to add/remove items, and use negative numbers 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. The new item to add to the array. var a8 = [' A ', ' B ', ' C ', ' d ']; var B8 = A8.splice (2,0, "Caibaojian") document.write (a8+ "<br/>"); Output a,caibaoijan,b,c,d alert (B8); var C8 = A8.splice (1,3); document.write (c8+ "<br/>"); document.write (A8); The sort () method is used to sort the elements of an array. Arrayobject.sort (SortBy) SortBy Optional. Specify the sort order. Must be a function. Returns a reference to the array of values. Note that the array is sorted on the original array and no replicas are generated. var a9 = [' A ', ' f ', ' h ', ' C ']; var B9 = A9.sort (); var C9 = [1,100,40,30,104,1000,4]; var D9 = C9.sort (); document.write ("<br/>" +b9+ "<br/>"); A,c,f,h according to the letter output document.write (a9+ "<br/>"); The original array has changed and the a,c,f,h is output in alphabetical order. document.write (d9+ "<br/>"); 1,100,1000,104,30,4,40 numbers are not output by size and need to be customized in one order function Sortnumber (a,b) { Return A-b } document.write (C9.sort (sortnumber) + "<br/>"); 1,4,30,40,100,104,1000 The Tosource () method represents the source code for the object. The original value is inherited by all objects derived from the Array object. The Tosource () method is usually invoked automatically in the background by JavaScript and does not appear explicitly in the code. Only Gecko's core browsers, such as Firefox, support the approach, IE, Safari, Chrome, Opera, and so on are not supported by browsers. Object.tosource () The ToString () method converts an array to a string and returns the result. Arrayobject.tostring () Return value: A string representation of the Arrayobject. The return value is the same as the string returned by the join () method without parameters. var A11 = [' A ', ' B ', ' C ', ' d ']; var B11 = a11.tostring (); document.write (b11+ "<br/>"); document.write ("The return value type of ToString is:" +typeof (B11)); Arrayobject.tolocalestring () The valueof () method returns the original value of the Array object. Arrayobject.valueof () </script> <a href= "Http://111cn.net" > cloud-Habitat Community Network </a> </body>
|