Straighten out JavaScript (11)-array

Source: Internet
Author: User
Tags array to string
Array direct quantity
 
VaR arr; arr = ['A', 'bb', 'cc']; alert (ARR. tolocalestring (); // AA, BB, ccarr = [11, 22, 33]; alert (ARR. tolocalestring (); // 11.00, 22.00, 33.00/* the array element can be of any type, including the array itself */ARR = ['A', 'bb ', 123]; alert (ARR. tolocalestring (); // AA, BB, 123.00 alert (ARR [0]); // aaalert (ARR [1]); // bbalert (ARR [2]); // 123/* The dimension can be set first, and then assigned a value */ARR = [,]; arr [0] = 11; arr [1] = 22; arr [2] = 33; alert (ARR. tolocalestring (); // 11.00, 22.00, 33.00/* First give an empty array, and then specify the Dimension */ARR = []; alert (ARR. length); // 0arr. length = 3; arr [0] = 11; arr [1] = 22; arr [2] = 33; alert (ARR. tolocalestring (); // 11.00, 22.00, 33.00/* The available Length attribute changes the array size */ARR = [,]; arr. length = 2; alert (ARR. tolocalestring (); // 1.00, 2.00

  

Create three types of array objectsCubeMethod

VaR arr; arr = new array (11, '22', true); alert (ARR. tolocalestring (); // 11.00, 22, truearr = new array (3); arr [0] = 11; arr [1] = '22 '; arr [2] = true; alert (ARR. tolocalestring (); // 11.00, 22, truearr = new array (); arr. length = 3; arr [0] = 11; arr [1] = '22'; arr [2] = true; alert (ARR. tolocalestring (); // 11.00, 22, true

  

Join: Array to string

 
VaR arr, STR; arr = new array ('A', 'B', 1, 2, 3); STR = arr. join (); alert (STR); // A, B, 1, 2, 3/* can specify the separator */STR = arr. join ('*'); alert (STR); // a * B * 1*2*3

  

Reverse, sort: Inversion and sorting

 
VaR arr; arr = new array ('C', 'D', 'E', 'A', 'B'); arr. reverse (); alert (ARR. tolocalestring (); // B, A, E, D, Carr. sort (); alert (ARR. tolocalestring (); // A, B, C, D, E

  

Concat: Add and return a new array

VaR arr; arr = [1, 2, 3] arr = arr. concat (4, 5); alert (ARR. join (); // 1, 2, 3, 4, 5

  

Push, pop: Add and delete elements from the end, return new length by push, and return deleted elements by pop.

 
VaR arr; arr = [1, 2, 3, 4, 5] arr. pop (); alert (ARR. join (); // 1, 2, 3, 4arr. push (7, 8, 9); alert (ARR. join (); // 1, 2, 3, 4, 7, 8, 9arr. push (0, ['A', 'B']); alert (ARR. join (); // 1, 2, 3, 4, 7, 8, 9, 0, A, Barr. pop (); alert (ARR. join (); // 1, 2, 3, 4, 7, 8, 9, 0arr. pop (); alert (ARR. join (); // 1, 2, 3, 4, 7, 8, 9

  

Unshift, shift: Insert and delete from the beginning; unshift returns the new length; shift returns the deleted Element

 
VaR arr; arr = [1, 2, 3] arr. unshift (5, 6); alert (ARR. join (); // 5, 6, 1, 2, 3arr. shift (); arr. shift (); alert (ARR. join (); // 1, 2, 3arr. unshift ([5, 6, 7, 8]); alert (ARR. join (); // 5, 6, 7, 8, 1, 2, 3arr. shift (); alert (ARR. join (); // 1, 2, 3

  

Splice: Delete, replace, and insert. If yes, the array of the deleted element is returned.

VaR arr; arr = [1, 2, 3, 4, 5, 6, 7, 8] arr. splice (4, 3); // delete three alert (ARR. join (); // 1, 2, 3, 4, 8arr. splice (3, 3, 'A', 'B', 'C'); // replace three alert (ARR. join (); // 1, 2, 3, a, B, Carr. splice (3, 0, 7, 8, 9); // insert 3 Records starting from 3rd (0); insert the second value to 0 alert (ARR. join (); // 1, 2, 3, 7, 8, 9, a, B, c

  

Slice: Extract sub-array; return new array

 
VaR arr; arr = [1, 2, 3, 4, 5, 6, 7, 8] arr = arr. slice (2, 5); alert (ARR. join (); // 3, 4, 5/* indicates the number of backend columns */ARR = [1, 2, 3, 4, 5, 6, 7, 8] arr = arr. slice (2,-2); alert (ARR. join (); // 3, 4, 5, 6arr = [1, 2, 3, 4, 5, 6, 7, 8] arr = arr. slice (-3,-1); alert (ARR. join (); // 6, 7

  

Tostring, valueof

VaR arr; arr = [1, 2, 3, 'A', 'B', 'C'] Alert (ARR); // 1, 2, 3, A, B, calert (ARR. tostring (); // 1, 2, 3, a, B, calert (ARR. valueof (); // 1, 2, 3, a, B, calert (ARR. tolocalestring (); // 1.00, 2.00, 3.00, a, B, c

  

Multi-dimensional array traversal and Simulation

 
VaR arr; arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]; alert (ARR [1] [1]); // 5arr = new array (1, 2, 3), new array (4, 5, 6), new array (7, 8, 9 )); alert (ARR [1] [1]); // 5 alert (ARR) // 1, 2, 4, 5, 6, 7, 8, 9for (I = 0; I <arr. length; I ++) {document. write (ARR [I] + '<B>'); //, 3/, 6/, 9} arr =, 9]; for (I in ARR) {document. write (ARR [I] + '<br>'); // 1/2/3/4/5/6/7/8/9} arr = new array ([, 3], [, 5, 6], [7, 8, 9]); For (I in ARR) {document. write (ARR [I] + '<br>'); //, 3/, 6/, 9} for (I in ARR) for (J in arr [I]) document. write (ARR [I] [J] + '<br>'); // 1/2/3/4/5/6/7/8/9

  

Related Article

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.