JS Array Method Array array declaration element additions and deletions, etc.

Source: Internet
Author: User

Declaring an array
    var arr1 = [1,2,3,4,5];              var New Array (+);  // declares a arr2 array of length 100. Arr2=[]; Arr2.length = +;    var New //is a two-dimensional array.  ARR3 = [[1,2,3,4,5]]; arr3.length = 1; arr3[0] = [1,2,3,4,5];arr3[0].length = 5;    var New Array (for each);    // ARR3 = [1,2];arr3.length = 2;
Before adding unshift () Remove shift () after adding push () to remove pop ()
    varARR1 = [1,2,3,4,5]; Arr1.unshift (5,6);//arr1 = [5,6,1,2,3,4,5]; Unshift: Adds one or more elements at the beginning of the collection and returns a new array    varARR2 = [1,2,3,4,5];           Arr2.shift (); //arr2 = [2,3,4,5] Shift: Removes the first element from the collection and returns the new array    varARR3 = [1,2,3,4,5]; Arr3.push (5,6);//ARR3 = [1,2,3,4,5,6] Push: The trailing element in the collection and returns the new array returned    varARR4 = [1,2,3,4,5];             Arr4.pop (); //ARR4 = [1,2,3,4] Pop: Removes the last element from the collection and returns a new array    varARR5 = [1,2,3,4,5]; Arr5.length= 3;//ARR5 = [n/a]; the array is quickly deleted and a new array is returned. 
The Intercept Arr.slice (start,end) method of the array start and end refers to the subscript, which does not contain an end element
 var  arr = [1,2,3,4,5,6,7,8,9 4) + ' <br/> '); //  output: 5,6,7,8,9  Document.writeln (Arr.slice ( -4) + ' <br/> '); //  output: 6,7,8,9  Document.writeln (Arr.slice (0,4) + ' <br/> '); //  output: 1,2,3,4  Document.writeln (arr.slice + ' <br/> '); //  output: 2  Document.writeln (Arr.slice (3,-2) + ' <br/> '); //  output: 4,5,6,7  Document.writeln (Arr.slice (1,100) + ' <br/> '); //  output: 2,3,4,5,6,7,8,9  

The slice (Start[,end]) method of the Array object returns the part of the array from the subscript [start,end] (the element that does not contain the subscript end) If the end parameter is not specified, the slice () method does not change the original array, starting with start to the end of the array. Can be assigned to a new array.
Parameters:

(1) Start: The array subscript that starts the intercept, if start is a negative number, indicates that the calculation starts at the end of the array.
(2) End: Ends the truncated array subscript, and if End is a negative number, the calculation begins at the tail of the array.

JS Array Method Array array declaration element additions and deletions, etc.

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.