Add, delete, and insert Javascript array elements and use of parameter Arrays

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.