Js array usage and array removal of elements based on subscript (value or character)
Source: Internet
Author: User
1. Create an array vararraynewArray (); vararraynewArray (size); specify the array length vararraynewArray (item1, item2 & hellip; itemN ); create an array and assign values 2. set values and assign values to varitemarray [index]. Obtain 1. Create an Array var array = new Array (); var array = new Array (size ); // specify the length of the array var Array = new array (item1, item2 ...... ItemN); // create an array and assign values 2. set values and assign values to var item = array [index]; // obtain the value of the specified element array [index] = value; // assign a value to the specified element 3. Add the new element array. push (item1, item2 ...... ItemN); // Add one or more elements to the array and return the length of array. unshift (item1, item2 ...... ItemN); // Add one or more elements to the starting position of the array. The original element position is automatically moved back and the length of the new array is returned. splice (start, delCount, item1, item2 ...... ItemN); // Delete delCount elements from the start position and insert one or more new elements from the start position. 4. Delete the element array. pop (); // Delete the last element and return the array of the element. shift (); // Delete the first element. The array element is automatically moved forward and the deleted element array is returned. splice (start, delCount); // Delete delCount elements from the start position. 5. Merge arrays and intercept arrays. slice (start, end); // returns a part of the array in the form of an array. Note that the elements corresponding to the end are not included. If the end is omitted, all elements after the start are copied to the array. concat (array1, array2); // concatenates multiple arrays into an array. 6. Sort arrays. reverse (); // array reverse array. sort (); // array sorting, returns array address 7, array to string array. Join (separator); // you can use separator to join the array. If so, no method is found to delete the array element according to the subscript! So I found some information and found the solution. To delete an Array element, You need to extend the Array prototype. in general, the subscript of an Array is numeric, but the subscript of the numeric type is also processed. First, write the following code to extend the Array. prototype. del = function (dx) {if (isNaN (dx) | dx> this. length) {return false;} this. splice (dx, 1);} second, the numeric type directly transmits the value parameter. For example, var arr = ["aa", "bb"]; arr. del (0); The subscript var arr = []. arr ["aa"] = 1;
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