Today, the common built-in object--array object in JS
Array Common Properties:
Length
Prototype: Adding properties and methods to system objects
Array Common methods:
Array.prototype.sum = function () {for (i=0;i<this.length;i++) {}}
Example
The code is as follows |
Copy Code |
Instance 1 <body> <script type= "Text/javascript" > var mycars = new Array () Mycars[0] = "Saab" MYCARS[1] = "Volvo" MYCARS[2] = "BMW" for (i=0;i<mycars.length;i++) { document.write (Mycars[i] + "<br/>") } </script> </body>
Instance 2 traverses the declared array with a for in <body> <script type= "Text/javascript" > var x var mycars = new Array () Mycars[0] = "Saab" MYCARS[1] = "Volvo" MYCARS[2] = "BMW" for (x in Mycars) { document.write (Mycars[x] + "<br/>") } </script> </body>
The concat statement is used to connect two arrays to output two consecutive arrays <body> <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> </body>
Add the string you want to add between elements of an array with the join () statement
<body> <script type= "Text/javascript" > var arr = new Array (3); Arr[0] = "George" ARR[1] = "John" ARR[2] = "Thomas" document.write (Arr.join ()); document.write ("<br/>"); document.write (Arr.join (".")); </script> </body> Sort output in alphabetical order in an array <body> <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 + "<br/>") document.write (Arr.sort ()) </script> </body>
Sort function in an array of sort numbers <body> <script type= "Text/javascript" > function Sortnumber (A, B) { Return A-b } Arrange in descending order var arr = new Array (6) Arr[0] = "10" ARR[1] = "5" ARR[2] = "40" ARR[3] = "25" Arr[4] = "1000" ARR[5] = "1" document.write (arr + "<br/>") document.write (Arr.sort (Sortnumber)) </script> </body>
|
Method
Concat () connects two or more arrays and returns the result
Join () puts all the elements of an array into a string. element is delimited by the specified delimiter
Pop () deletes and returns the last element of the array
Push () adds one or more elements to the end of the array and returns a new length
Unshift () adds one or more elements to the beginning of the array and returns a new length
Reverse () Reverses the order of elements in an array
Shift () deletes and returns the first element of the array
Slice () returns the selected element from an existing array
Splice (Index,howmatch,elem) deletes the element and adds a new element to the array (delete from index, delete howmatch, then add element Elem)
Sort () array sorting
ToString () converts the array to a string and returns the result