1. Add array elements
The push method adds elements at the end of the array:
VaR colorarray = new array ();
Colorarray. Push ('red', 'black', 'yellow'); // press the three elements directly.
// Of course, this can also be written.
Colorarray. Push ('red ');
Colorarray. Push ('black ');
2. Deletion and insertion of array elements
The pop method deletes the last element of the array:
VaR colorarray = new array ();
Colorarray. Push ('red', 'black', 'yellow ');
Colorarray. Pop (); // Delete the last element of the array.
VaR item = colorarray. Pop (); // Of course, this method can return the last element of the array while deleting it.
The second method for deleting array elements is to use splice ()
Splice () can delete any number of items. You only need to input two parameters: the location of the first item to be deleted and the number of items to be deleted. For example, splice
(0, 2), delete the first two items in the array. Of course, splice (2, 2) is to delete the array element whose subscript is 2.
Splice () can also insert an array:
Splice (, 'xiaochun'): three parameters are input here. The first parameter is the location to be inserted, and the second parameter indicates the number of elements to be deleted (0 here, the third parameter indicates the data to be inserted. The running result is to insert xiaochun at subscript 2.
The above instances can also be replaced:
Splice (, 'xiaochun') indicates that one element is deleted at marked 2 and then xiaochun is inserted. This is equivalent to a replacement.
3. Use of parameter Arrays
In JavaScript, the function parameter is actually an array:
Function Test () {// note that no function parameter is provided here
Alert (arguments [0] + "," + arguments [1] + "," + arguments [2]);
For (VAR I = 0; I <arguments. length; I ++)
{
Alert (I );
}
}
Test ('xiaochun', 'xiaoming', 'xiaozhang'); // here, three parameters are input to the function.
Run this program and you will find that it outputs all the parameters, which means that the parameters of the JS function are actually an array by default.