One, use index subscript to read the value of the array
var box = new Array (' Li Tinghui ', 28, ' teacher ', ' Yancheng '); Alert (box[2]); Get the third element box[2] = ' student '; Modification of the third element box[4] = ' computer programming '; Add Fifth Element alert (box[4])
Second, use the length property to get the array element amount
var box = new Array (' Li Tinghui ', 28, ' teacher ', ' Yancheng '); Alert (box.length)//Gets the number of elements box.length = 10; Force the number of elements, PS: An array can contain up to 4,294,967,295 elements, and an exception occurs beyond that. Box[box.length] = ' JS technology ';//Add an element to the array by length, adding an element to the last corner label
Third, the conversion method
1. Objects or arrays have tolocalestring (), toString (), and valueof () methods.
2, where ToString () and valueof () will return the same value regardless of who rewritten it.
3, the array will each value in string form concatenation, separated by commas.
var // literal array // the toString () is implicitly called // and ValueOf () returns the consistent // return value and above
var box = [New Date ()];alert (box.tostring ()); Tue Mar 17:14:28 Gmt+0800alert (box.tolocalestring ());//has local formatted functionality and does not automatically invoke the result is March 25, 2014 17:14:28
Iv. Join () method
By default, array strings are separated by commas. If you use the Join () method, you can use a different delimiter to construct the string.
var box = [' Li Tinghui ', 28, ' computer Programming '];alert (box.join (' | '));//EON Restore |28| computer programming The returned type is a string
Five, stack method
1. The ECMAScript array provides a way for the array to behave like other data structures. That is, you can make an array like a stack, which restricts the data structure of the inserted and deleted items.
2, the stack is a data structure (LIFO), that is, the newly added element was first removed. The insertion (or push-in) of elements in the stack and the removal (or popup) are only in one place-the top of the stack.
3. ECMAScript provides the push () and Pop () methods specifically for the array:
The push () method can receive any number of arguments, add them to the end of the array one by one, and return the length of the modified array.
The Pop () method removes the last element from the end of the array, reduces the length value of the array, and then returns the removed element.
var // Literal declaration // adds an element to the end of the array and returns the length // View Arrays // removes the element at the end of the array and returns the removed element // viewing elements
Vi. Queue Methods
1, the Stack method is LIFO, and the queue method is FIFO.
2. The queue adds elements at the end of the array, removing elements from the front end of the array.
3. Add an element to the end of the array via push () and remove an element from the front end of the array through the shift () method.
var // Literal declaration // adds an element to the end of the array and returns the length // View Arrays // remove an array from the beginning element and return the removed element // View Arrays
JavaScript also provides an array of unshift () methods, which are the exact opposite of the shift () method. The Unshift () method adds an element to the front end of the array.
var // Literal declaration // add two elements to the beginning of the array // View Arrays // removes the element at the end of the array and returns the removed element // View Arrays
Vii. Method of sorting
There are already two methods in the array that can be used to sort directly: reverse () and sort ()
1, reverse () reverse order
var // Array // Reverse Sort method, returns the sorted array // The source array is also reversed, indicating that the reference
2. Sort () from small to large
var // Array // sort from small to large, return sorted array //
The default ordering of the sort method has some problems with numeric sorting because the algorithm for sorting numbers and numbers is the same.
We have to modify this feature and modify it by passing a function parameter to the sort (parameter) method.
functionCompare (value1, value2) {//function parameters for numeric sorting if(Value1 < value2) {//less than, returns a negative number return-1; } Else if(Value1 > value2) {//greater than, returns a positive number return1; } Else{//others, return 0 return0; }}varbox = [0,1,55,10,65];//Verify the difference between a numeric string and a numberAlert (Box.sort (compare));//Pass the reference
If you want to reverse the operation, that is, from the large to the small sort, positive or negative reversal. Of course, it is more convenient to use reverse () in reverse order.
Viii. Other methods of operation:
1. The concat () method can create a new array based on the current array.
var // Current Array var // create a new array and add new elements // output New Array // There are no changes to the current array
2. The slice () method can get the specified range element based on the current array and create a new array.
var // Current Array var box2 = Box.slice (1); // A parameter indicates that the parameter is taken from the beginning of the // var box3 = Box.slice (1,3); elements between//2-4 two parameters represent the start of the first argument, take the position of the second argument //28, Yancheng // Current Array
3, Splice () The main purpose is to insert elements into the array.
Splice in the Delete function:
var // Current Array var // intercepts the first two elements, which means two elements are intercepted starting from 0 // returns the intercepted element // the current array of intercepted elements is deleted
Insert function in splice (operation is the original array):
var // Current Array var // no interception, but inserted two 1 means inserted from the 1th-point mark, 0 means not to delete, if the element is changed to 1,28 no // Nothing, because the second parameter is 0, no intercept // The output is inserted in the 2nd position with two bars
Replacement features in splice:
var // Current Array var // intercept the 2nd, replace with the second parameter in a few to intercept a few / / output Array
How to manipulate the array of JavaScript